devork

E pur si muove

Tuple unpacking goodness

Sunday, November 29, 2009

Todays pleasant surprise:

>>> a = {'a': (0, 1), 'b': (2, 3)}
>>> for k, (v1, v2) in a.iteritems():
...     print k, v1, v2
... 
a 0 1
b 2 3
>>>

Nice!

Sunday, November 29, 2009 | Labels: |

2 comments:

Marius Gedminas said...

Did you know that this also works for function calls (on Python 2.x; feature removed in Python 3.x)?

def f((x1, y1), (x2, y2)):
    print x1, y1
    print x2, y2

zero = (0, 0)
f(zero, (1, 2))

Anonymous said...

Unfortunatly this feature has been removed from python3 (probably a locak of use).

New comments are not allowed.

Subscribe to: Post Comments (Atom)