looping throuhg a list 2 items at a time
Fernando Pérez
fperez528 at yahoo.com
Wed Apr 10 20:01:09 EDT 2002
Rajarshi Guha wrote:
> Hi,
> I have a list like this: l = [1,'s', 2,'t', 3,'d']
>
> I'd like to loop through the list but within the loop I want
> the current element and the element just after it:
> I tried this
>
> liter = iter(l)
> for i in l:
> print i, liter.next()
This works (notice you need to iterate over the _iterator_ for this to work):
In [9]: liter = iter(l)
In [10]: for a in liter:
....: print a,liter.next()
....:
1 s
2 t
3 d
cheers,
f.
More information about the Python-list
mailing list