One-step multiples list generation?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Jan 3 19:13:39 EST 2006


On Tue, 03 Jan 2006 19:21:32 +0100, Damien Wyart wrote:

> Thanks for these important and useful additions, they are very welcome !
> 
> In writing my answer I had immutables in mind, but mutables are a bit
> more dangerous, here...

Mutables are easy to deal with using the copy module and list
comprehensions:

>>> foo = []
>>> import copy
>>> multi_foo = [copy.copy(foo) for _ in range(10)]
>>> multi_foo
[[], [], [], [], [], [], [], [], [], []]
>>> multi_foo[5].append(None)
>>> multi_foo
[[], [], [], [], [], [None], [], [], [], []]

Or use copy.deepcopy if you need to.



-- 
Steven.




More information about the Python-list mailing list