Initializing a list with copies
Ralf Juengling
juenglin at informatik.uni-freiburg.de
Fri Apr 26 04:57:10 EDT 2002
Hi,
does anybody see an easy way to do the following:
I need a list of initially empty lists that are to be
filled, l say. The parameter n says how many empty
lists l should contain. Someone tried
>>> l = [[]]*n
to create l. However, this is not what he wanted:
>>> n
2
>>> l
[[], []]
>>> l[0].append(1)
>>> l
[[1], [1]]
To create an l with inidividual empty lists, he now
does
>>> l = [[] for i in range(n)]
Can this be done more elegant? By some expression that
was similar to '[[]]*n' ?
Thanks,
Ralf
More information about the Python-list
mailing list