Showing posts with label tuples. Show all posts
Showing posts with label tuples. Show all posts

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

Monday, May 12, 2008

Python tuple unpacking within a loop

Python tuple unpacking gets rid of tuple indices and temporary variables.

>>> a = range(11, 15)
>>> b = zip(range(21, 25), range(31, 35))
>>> for x, (y, z) in zip(a, b):
... print x, y, z
...
11 21 31
12 22 32
13 23 33
14 24 34

Thursday, May 8, 2008

Definitional tuple-packing in Python

Python functions may tuple-pack their arguments.

def foo(a, (b, c)):
print a, b, c

>>> foo(10, (20, 30))
10 20 30

Arguments unpack during evaluation.