References vs copies w/ *x
Duncan Booth
duncan at rcp.co.uk
Fri Oct 1 09:40:00 EDT 1999
emile at fenx.com (Emile van Sebille) wrote in
<tl1J3.1900$r8.63155 at news.direcpc.com>:
>>>> a = [1,2,3]
>>>> b = [[]]*3
>>>> b = map(lambda x:a[:],b)
>>>> b[1][1] = 'c'
>>>> b
>[[1, 2, 3], [1, 'c', 3], [1, 2, 3]]
>
>
How about a slightly simpler variant:
>>> a = [1, 2, 3]
>>> b = map(list, [a]*3)
>>> b[1][1] = 'c'
>>> b
[[1, 2, 3], [1, 'c', 3], [1, 2, 3]]
or you can import copy and use copy.copy in the map, but using list avoids
the import.
More information about the Python-list
mailing list