Initializing a list with copies

Bill Dozier billdozier at hotmail.com
Fri Apr 26 10:45:45 EDT 2002


Ralf Juengling <juenglin at informatik.uni-freiburg.de> wrote in message news:<xduy9fa4z2x.fsf at leto.informatik.uni-freiburg.de>...


> >>> n

>  2

> >>> l

>  [[], []]

> >>> l[0].append(1)

> >>> l

> [[1], [1]]


This looks like the old "mutable initializer" thingie.

> >>> l = [[] for i in range(n)]

> Can this be done more elegant? 
"[[] for i in range(n)]" looks fine to me.

> By some expression that was similar to '[[]]*n' ? 
Do you mean, "with few characters to type?" That's not the same as
"with more elegance."

Bill



More information about the Python-list mailing list