[Tutor] difference between [[], [], []] and 3*[[]]

Mitsuo Hashimoto hashimoto.m at gmail.com
Mon May 23 01:55:35 CEST 2005


2005/5/21, Kent Johnson <kent37 at tds.net>:

> 3*[[]] makes a list with three references to the *same* list. This can cause surprising behavior:
>
>   >>> l=3*[[]]
>   >>> l
> [[], [], []]
>   >>> l[0].append(1)
>   >>> l
> [[1], [1], [1]]

I see.

> Often using a dict is a good solution to this type of question.

I didn't notice a dict.

Thank you,

Mitsuo


More information about the Tutor mailing list