Loop over list of pairs

Peter Hansen peter at engcorp.com
Thu Jun 5 14:11:20 EDT 2003


Thomas Güttler wrote:
> 
> What is the prefered way of loop over
> a list like this?
>  mylist=[1, "one", 2, "two", 3, "three"]


>>> def divide(s, n):
...   return [s[i:i+n] for i in xrange(0, len(s), n)]
...
>>> mylist = [1, 'one', 2, 'two', 3, 'three']
>>>
>>> for left, right in divide(mylist, 2):
...   print left, 'is', right
...
1 is one
2 is two
3 is three


-Peter




More information about the Python-list mailing list