Monday, May 17, 2010

Copying lists with Python

For single lists, the "[:]" would do (reference e.g. at http://henry.precheur.org/python/copy_list).

For nested lists, I think a module should be used (copy.deepcopy(list)), as in:

>>> import copy
>>> a
[[5, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> c = copy.deepcopy(a)
>>> c
[[5, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> c[0][0] = 7
>>> c
[[7, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> a
[[5, 2, 3], [1, 2, 3], [1, 2, 3]]
>>>

Categories

code (15) fun (2) linux (31) miscellaneous (9) music (8)

About me

I'm Adrian and this is my blog. Here I usually write about technical stuff (mostly about Linux).
Copyright: BY-NC_SA, Author: aeter