idiom for initial list of lists
Dan Schmidt
dfan at harmonixmusic.com
Fri Sep 8 13:53:41 EDT 2000
Robin Becker <robin at jessikat.fsnet.co.uk> writes:
| What's the correct way to initialise a list of lists I find that the
| natural [[]]*n doesn't work eg
| >>> S=[[]]*5
| >>> S
| [[], [], [], [], []]
| >>> S[0].append(1)
| >>> S
| [[1], [1], [1], [1], [1]]
|
| so I'm forced to use the rather pedantic
| S=[]
| for i in range(n): S[i].append([])
How about
S = map (lambda x : [], range(5))
Or in Python 2.0,
S = [[] for x in range(5)]
--
Dan Schmidt | http://www.dfan.org
Honest Bob CD now available! | http://www.dfan.org/honestbob/cd.html
More information about the Python-list
mailing list