Python: how to add new items to a list list?

Here is a very simple program:

a = [[]]*3 print str(a) a[0].append(1) a[1].append(2) a[2].append(3) print str(a[0]) print str(a[1]) print str(a[2]) 

Here is the result I was expecting:

  [[], [], []] [1] [2] [3] 

But instead, I get the following:

  [[], [], []] [1, 2, 3] [1, 2, 3] [1, 2, 3] 

There is something that I do not see here!

+6
source share
5 answers

You have to do

 a = [[] for i in xrange(3)] 

not

 a = [[]]*3 

Now it works:

 $ cat /tmp/3.py a = [[] for i in xrange(3)] print str(a) a[0].append(1) a[1].append(2) a[2].append(3) print str(a[0]) print str(a[1]) print str(a[2]) $ python /tmp/3.py [[], [], []] [1] [2] [3] 

When you do something like a = [[]]*3 , you get the same list [] three times in the list. The same means that when you change one of them, you change all of them (because there is only one list that is referenced three times).

You need to create three independent lists to work around this problem. And you do it with a list. Here you create a list consisting of independent empty lists [] . A new empty list will be created for each iteration on xrange(3) (the difference between range and xrange is not so important in this case, but xrange slightly better because it does not give a complete list of numbers and just returns an iterator object).

+12
source

When you write:

 a = [[]]*3 

You will not make 3 copies of the internal empty list, but instead you will make 3 links to the same object.

Similarly, if you do this:

 b = [1,2,3] c = b c.append(4) print b 

You get as a result:

 [1, 2, 3, 4] 

This is because b and c are two different links (you can say two different names) to the same list. You can change the object from any link, but you will see results from all of them, since they point to the same thing.

+5
source

The main problem with your approach is that when you multiply the list by three, the resulting list contains three identical lists. So, a[0] , a[1] and a[2] all refer to the same thing. When you join any of them, you join the same list. This is why the effect of your append() calls seems complicated.

Instead, create a list of lists without reference to the same list. For example, you can use list comprehension, for example.

 [[] for i in range(3)] 
+5
source

this will give you a clear idea: in l, all objects have the same identifier (), and all of them are mutable, so editing any of them will automatically change the others, since all of them simply refer to the same object with id = 18671936 and in m everything have different id (), so they are all different objects.

 >>> l = [[]]*4 >>> for x in l: print(id(x)) 18671936 18671936 18671936 18671936 >>> m=[[],[],[],[]] >>> for x in m: print(id(x)) 10022256 18671256 18672496 18631696 

So you should create your list as follows:

 >>> a=[[] for _ in range(4)] >>> a[0].append(1) >>> a[2].append(5) >>> a [[1], [], [5], []] 
+2
source

There is something that I do not see here!

a = [[]]*3 problem is that you are setting the lists as the same link, since arrays as well as dictionaries are stored as links.

 >>> a = [[]]*3 >>> a[0] is a[1] or a[0] is a[2] True >>> a[0] is a[1] or a[0] is a[2] or a[1] is a[2] True >>> 

The same thing happens if you do a = [{}] * 3`

as such you should be careful, you can either comprehend the list [[] for _ in xrange(3)] or statically define all 3 of your arrays [[], [], []] .

 >>> a = [[] for _ in xrange(3)] >>> a[0] is a[1] or a[0] is a[2] or a[1] is a[2] False >>> 
+1
source

Source: https://habr.com/ru/post/919092/


All Articles