[issue15416] 3 * [] gives a list of 3 cross-referenced lists, a[1]='blash

Martin Mokrejs report at bugs.python.org
Sat Jul 21 23:54:35 CEST 2012


New submission from Martin Mokrejs <mmokrejs at users.sourceforge.net>:

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

----------
components: None
messages: 166081
nosy: mmokrejs
priority: normal
severity: normal
status: open
title: 3 * [] gives a list of 3 cross-referenced lists, a[1]='blash
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15416>
_______________________________________


More information about the Python-bugs-list mailing list