This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 3 * [] gives a list of 3 cross-referenced lists, a[1]='blah' writes in to ALL three!
Type: behavior Stage:
Components: None Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, mmokrejs, r.david.murray
Priority: normal Keywords:

Created on 2012-07-21 21:54 by mmokrejs, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg166081 - (view) Author: Martin Mokrejs (mmokrejs) Date: 2012-07-21 21:54
Hi,
  I thought that I can easily create a list of, say 3, nested lists:

$ python
Python 2.7.3 (default, May 17 2012, 21:10:41) 
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 * [[]]
[[], [], []]
>>> a=3 * [[]]
>>> a[0]
[]
>>> a[1].append('sdds')
>>> a
[['sdds'], ['sdds'], ['sdds']]
>>> a[0].append('xxx')
>>> a
[['sdds', 'xxx'], ['sdds', 'xxx'], ['sdds', 'xxx']]


The below works as expected.

>>> a=[[], [], []]
>>> a
[[], [], []]
>>> a[0]
[]
>>> a[1]
[]
>>> a[1].append('sdds')
>>> a[0].append('xxx')
>>> a
[['xxx'], ['sdds'], []]
>>>


Of course, I want to do like:

a = len(b) * [] # to get a list of individual hashes
c = len(b) * [0] # to get a list of indvidual counters
msg166085 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2012-07-21 22:12
The * operator does not create copies, it duplicates references to existing objects.
Please read: http://docs.python.org/faq/programming.html#how-do-i-create-a-multidimensional-list
msg178995 - (view) Author: Martin Mokrejs (mmokrejs) Date: 2013-01-03 23:11
For the sake of internet archives, the following could be included in the FAQ you referred to:

http://www.scipy.org/Cookbook/BuildingArrays

>>> import numpy as np
>>> a=np.array(5*[False],bool)
>>> a
array([False, False, False, False, False], dtype=bool)
>>> a[4]=1
>>> a
array([False, False, False, False,  True], dtype=bool)
>>>

But thanks for the link, it helped me at that time.
msg178996 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-03 23:23
But that's about numpy, which is not part of core Python, and so doesn't belong in a Python FAQ.  But perhaps your posting it here will help someone doing an internet search...
History
Date User Action Args
2022-04-11 14:57:33adminsetgithub: 59621
2013-01-03 23:23:26r.david.murraysetnosy: + r.david.murray
messages: + msg178996
2013-01-03 23:11:05mmokrejssetmessages: + msg178995
2012-07-21 22:12:48amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg166085

resolution: not a bug
2012-07-21 21:54:58mmokrejssettitle: 3 * [] gives a list of 3 cross-referenced lists, a[1]='blash -> 3 * [] gives a list of 3 cross-referenced lists, a[1]='blah' writes in to ALL three!
2012-07-21 21:54:34mmokrejscreate