Creating a List of Empty Lists

Daniel Dittmar daniel.dittmar at sap.com
Thu Dec 4 10:02:16 EST 2003


Fuzzyman wrote:
> Pythons internal 'pointers' system is certainly causing me a few
> headaches..... When I want to copy the contents of a variable I find
> it impossible to know whether I've copied the contents *or* just
> created a new pointer to the original value....
>
> For example I wanted to initialize a list of empty lists....
>
> a=[ [], [], [], [], [] ]
[...]
> What is the correct, quick way of doing this (without using a loop and
> appending...) ?


>>> l = [ [] for i in xrange (3)]
>>> l
[[], [], []]
>>> l [0].append ('a')
>>> l
[['a'], [], []]

Daniel







More information about the Python-list mailing list