using "*" to make a list of lists with repeated (and independent) elements

Ian Kelly ian.g.kelly at gmail.com
Wed Sep 26 17:39:32 EDT 2012


On Wed, Sep 26, 2012 at 3:20 PM, TP <TP at frenoespam.fr.invalid> wrote:
> Hi everybody,
>
> I have tried, naively, to do the following, so as to make lists quickly:
>
>>>> a=[0]*2
>>>> a
> [0, 0]
>>>> a[0]=3
>>>> a
> [3, 0]
>
> All is working fine, so I extended the technique to do:
>
>>>> a=[[0]*3]*2
>>>> a
> [[0, 0, 0], [0, 0, 0]]
>>>> a[0][0]=2
>>>> a
> [[2, 0, 0], [2, 0, 0]]
>
> The behavior is no more expected!
> The reason is probably that in the first case, 0 is an integer, not a list,
> so Python copies two elements that are independent.
> In the second case, the elements are [0,0,0], which is a list; when Python
> copies a list, he copies in fact the *pointer* to the list, such that we
> obtain this apparently strange behavior.

Mostly correct.  When you do [foo] * 3 it extends the list with the
*same objects* no matter what type they are.  In the case of integers,
it doesn't matter that it's the same objects, because integers are
immutable.  Lists are mutable, however, and so it becomes apparent
that the same objects are repeated when you try to modify one of the
lists.

> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*"
> without this behavior?

Use a list comprehension:

a = [[0] * 3 for _ in range(2)]

This way the expression `[0] * 3` is re-evaluated at each position in
the outer list, rather than evaluated just once and then copied.



More information about the Python-list mailing list