[Tutor] Re: List Comprehension (shared references)
pan@uchicago.edu
pan@uchicago.edu
Fri Apr 25 00:54:02 2003
> 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.
Try this:
>>> x=7
>>> m = []
>>> ml =[ m for i in range(x)]
>>> ml
[[], [], [], [], [], [], []]
>>> ml[1].append('test')
>>> ml
[['test'], ['test'], ['test'], ['test'], ['test'], ['test'], ['test']]
pan