216
votes

I intend to initialize a list of list with length of n.

x = [[]] * n

However, this somehow links the lists together.

>>> x = [[]] * 3
>>> x[1].append(0)
>>> x
[[0], [0], [0]]

I expect to have something like:

[[], [0], []]

Any ideas?

1
Both questions reference the same code construct: x = [ [ ] ] * n followed by x[ n ].append( y ) and show the same confusion over the behavior x[ n ] == y for all values of n. It could not more clearly be a duplicate. And yet search engine sent me here first. ;) - user2895783
This question is about how to initialize a list of independent lists. The other question is about why the [ [ ] ] * n results in "linked" new list behavior. The answers are different, and for those seeking how to do what they want, this is the right question/answer. This is not a duplicate at all, @Loveandpeace-JoeCodeswell is right. - Greg Kramida
Note: this was reopened and closed solely for the purposes of fixing the old closure. Do not take my closure as an endorsement of the closure itself. - Machavity♦

1 Answers

339
votes

The problem is that they're all the same exact list in memory. When you use the [x]*n syntax, what you get is a list of n many x objects, but they're all references to the same object. They're not distinct instances, rather, just n references to the same instance.

To make a list of 3 different lists, do this:

x = [[] for i in range(3)]

This gives you 3 separate instances of [], which is what you want

[[]]*n is similar to

l = []
x = []
for i in range(n):
    x.append(l)

While [[] for i in range(3)] is similar to:

x = []
for i in range(n):
    x.append([])   # appending a new list!

In [20]: x = [[]] * 4

In [21]: [id(i) for i in x]
Out[21]: [164363948, 164363948, 164363948, 164363948] # same id()'s for each list,i.e same object


In [22]: x=[[] for i in range(4)]

In [23]: [id(i) for i in x]
Out[23]: [164382060, 164364140, 164363628, 164381292] #different id(), i.e unique objects this time