Python List Confusion

Python list confusion

Your hunch about copying addresses is correct. Think about it like this:

sub_list = [0] * 10
a_list = [sub_list] * 10

This code is actually equivalent to the code you have posted above. What this means is that you are actually changing the same list sub_list whenever you change any element of a_list. You can even make sure of it by typing:

a_list = [[0] * 10] * 10
for n in a_list:
print id(n)

And it will show up the same for every element. To remedy this, you should use:

a_list = [[0] * 10 for _ in range(10)]

In order to create a new sublist for every element of a_list.

Python list reversing function confusion

As the juanpa.arrivillaga's comment,

Because x = x[::-1] doesn't mutate the list in-place. It creates a new list and assigns it to the local variable x. That list is discarded when the function terminates. On the other hand, x[:] = whatever does mutate the list. Indeed, x[:] = x[::-1] actually creates a new list, and that new list is used to mutate the original list

And you could use the built-n method .reverse() to reverse a list

mylist = [1,2,3]
mylist.reverse()
print(mylist)

Or you could use reversed:

mylist = [1,2,3]
mylist = reversed(mylist)
print(list(mylist))

And try to avoid using built-in function name list in variable names.

Python lists confusion

In li2 = [['a']] * 3 you create one list with three list elements but these lists are actually the same object. That means: when you modify li2[0], you also modify li2[1] and li2[2].

The following line actually creates a list with three different list objects inside it:

li1 = [['a'], ['a'], ['a']]

In this case, when you modify li1[0] you only modify that list. The other lists are unaffected. This explains why you are getting different lists in li1 and li2.

list() function confusion

With your list called path_files, this list is actually a list within a list. You can see this because of the double square brackets on the end of your list, e.g. [['combinedPdfs.py', ..., 'readDocx.py']]. A single list would have only one set of square brackets.

The command >>> path_files = list(path_files)[0], essentially, returns the first item in the list broken out as a list. So, in the case of the ['hello'] list, it broke out hello into separate characters. With your list, it broke out the first item in the list (in this case, another list) and broke it out into its own list.

While I cannot see where you defined path_files_2 (I am assuming it is the same as the new path_files), when you type path_files_2[0] you are saying to return the first item in the list (zero-indexed).

I generally don't think of it as "the list function" but rather "the list datatype" that has its own methods. When you type list(...), I don't think of it as a "function" but rather as a method of data type casting one datatype into a list datatype.

Confusion in the Python list output

You create two lists and use them to create a list of lists:

number=[even,odd] = [[2,4,6,8], [1,3,5,7,9]]

Then you loop over this list of lists, so i contains each member of this list of lists, which is a list.

for i in [[2,4,6,8], [1,3,5,7,9]]:
print(i)

>> [2, 4, 6, 8]

Then you loop over each element of this list. Each element is stored in j.

for j in [2, 4, 6, 8]:
print(j)

>> 2
>> 4
>> 6
>> 8

Python Dictionary and list confusion

I think this is what you meant to do:

wardrobe = {"shirt":["red","blue","white"], "jeans":["blue","black"]}

for cloth, colors in wardrobe.items():
for color in colors:
print("{} {}".format(color, cloth))

Confused with nested list

The issue on your code is related to shallow and deep copies. When your code assigned visited by doing:

visited = [[False] * len(maze[0])] * len(maze)

What it's really doing is first creating a list of False (and this is ok, as False is a value type), but then scripting two copies of the list. Lists are objects, so the result is a list with two references to the same list of False. Then when you changed the first inner list, it also changed the second list's values since both were the same.

A fix for this is to change the construction of visited by using a list comprehension to generate two copies of the inner list:

visited = [[False] * len(maze[0]) for n in range(len(maze))]

Then, the full code will be:

def start_matrix(maze):
visited = [[False] * len(maze[0]) for n in range(len(maze))]
print(visited)
for i in maze:
for k in i:
if k == '=' or k == '|':
print(maze.index(i), i.index(k))
print(visited[maze.index(i)][i.index(k)])
visited[maze.index(i)][i.index(k)] = True
print(visited)
return None

map1 = [['=', ' ', '|'], [' ', ' ', ' ']]
start_matrix(map1)

With an output of:

[[False, False, False], [False, False, False]]
0 0
False
[[True, False, False], [False, False, False]]
0 2
False
[[True, False, True], [False, False, False]]

Hope I'm being clear. If not, please let me know and I'll expand.



Related Topics



Leave a reply



Submit