Need help initializing a list or tuple.

cdunn2001 at gmail.com cdunn2001 at gmail.com
Mon Mar 6 23:26:09 CET 2006


# Nested list comprehensions act like real for loops:
>>> a = [[None for i in range(3)] for j in range(3)]
>>> a
[[None, None, None], [None, None, None], [None, None, None]]
>>> a[0][0] = 5
>>> a
[[5, None, None], [None, None, None], [None, None, None]]
# Side-effect: i and j will be changed.

# Another way (much less clear to my eyes):
>>> a = map(lambda x: map(lambda x: None, range(3)), range(3))
>>> a[0][0]=5
>>> a
[[5, None, None], [None, None, None], [None, None, None]]




More information about the Python-list mailing list