[Tutor] List Comprehension (shared references)

Tim Johnson tim@johnsons-web.com
Thu Apr 24 23:51:01 2003


Hi all:
I'm just getting my feet wet with list comprehensions.
I need to write a function that returns a nested list
of empty lists.
It seems simple, but I'm a python newbie and I don't
want to leave myself that can bite me later...

The following console session seems to fit the bill.
x = 7
>>> ml = [[] for row in range(x)]
>>> ml
[[], [], [], [], [], [], []]

# Now that *seems* to be what I'm looking for :-)
And furthermore:
>>> ml[1].append('test')
>>> ml
[[], ['test'], [], [], [], [], []]

There appears to be no shared references.

Am I correct here?
Comments appreciated. 
-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com
      http://www.johnsons-web.com