Saturday, October 31, 2009

Python list copy, Python tuple copy

Full-slicing a list builds a new list:

>>> old = [1, 2, 3]
>>> new = old[:]
>>> old is new
False


But full-slicing a tuple does NOT build a new tuple:

>>> old = (1, 2, 3)
>>> new = old[:]
>>> old is new
True

1 comment:

Jared Grubb said...

Tuple is immutable. Therefore, it skips the copy because it doesnt matter if it's a new object or not.