[issue1408] Inconsistence in multiply list

Georg Brandl report at bugs.python.org
Fri Nov 9 14:19:47 CET 2007


Georg Brandl added the comment:

I'm sorry, this is no bug. List multiplication works by referencing,
there is no way to implement it differently in a straightforward way.

Note that in
>>> [a[:]] + [a[:]]
the expression "a[:]" is evaluated twice, yielding two independent
copies of a. In contrast,
>>> [a[:]] * 2
evaluates "a[:]" only once, before the list multiplication is done.
Because of the same reason,
>>> [deepcopy(a)] * 2
doesn't work as you want.

One way to do what you have in mind is
>>> [a[:] for i in range(2)]
which evaluates the "a[:]" once for each iteration of the list
comprehension loop.

----------
nosy: +georg.brandl
resolution:  -> invalid
status: open -> closed

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1408>
__________________________________


More information about the Python-bugs-list mailing list