Andy McKay

Jun 28, 2008

One of those features I forgot about

Always nice to remember a feature in Python that I haven't used for ages. This happened to me the other day with this... given a list:

x = ["hair", "brown", "eyes", "blue"]

How do I get the hair and eye colours easily? One way, the oft forgotten 3rd argument to list slicing:

Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
>>> x = ["hair", "brown", "eyes", "blue"]
>>> for z in zip(x[::2], x[1::2]):
...   print z
('hair', 'brown')
('eyes', 'blue')