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:

>>> x = ["hair", "brown", "eyes", "blue"]
>>> for z in zip(x[::2], x[1::2]):
...   print z
('hair', 'brown')
('eyes', 'blue')