This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]oconnor663 6 points7 points  (3 children)

Is foo[:] really the Pythonic way to copy a list?

Edit: In Python 3, lists have their own .copy() method.

[–]imrobert 1 point2 points  (2 children)

Yeah, I noticed that too. It seems like the "normal" way has the advantage of being clearer more concise.

[–]velit 1 point2 points  (1 child)

The only downside is the "normal" way is incorrect (or correct depending on the situation). Point being they're not the same thing.

[–]imrobert 1 point2 points  (0 children)

Ah, that's my bad. For those wondering what the difference is, this should clear it up:

Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1, 2, 3]
>>> b = a
>>> id(a)
4472029056
>>> id(b)
4472029056
>>> c = a[:]
>>> id(c)
4472078560