how to make a nested list

Peter Pearson ppearson at nowhere.invalid
Thu Sep 15 19:17:42 CEST 2011


On Thu, 15 Sep 2011 18:57:24 +0200, Stef Mientki wrote:
[snip]
> Because the list is much larger, I need a shortcut (ok I can use a for loop)
> So I tried
> >>> B = 3 * [ [ None, None ]]
> >>> B[2][0] = 77
> >>> B
> [[77, None], [77, None], [77, None]]
>
> which doesn't work as expected.
>
> any suggestions ?

>>> b = [[None, None] for i in range(3)]
>>> b[2][0] = 77
>>> b
[[None, None], [None, None], [77, None]]

If you don't understand why the other approach resulted in
[[77, None], [77, None], [77, None]], feel free to ask.  It's
a mistake that I think everybody makes at least once.

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list