[Zope] how to iterate through two sequence items

Jo Meder jo@meder.de
Fri, 8 Feb 2002 19:14:49 +0100


Am 08.02.2002, 12:18 Uhr
	schrieb Peter Walerowski
	<peter.walerowski@berlin.de>:

> it would be nice to have the possibilty to iterate through two or more sequence items in one pass, wouldn't it?

Hai Peter!

That's easily possible if you manipulate your list to reflect your data
processing requirements. You want to have to items in one iteration so
IMHO the pythonic solution is not to skip every other item but to
construct a list of tuples each containing two items.

Assuming your list looks like this:

m=["a","b","c","d","e","f"]

you can do this by:

newlist=map(lambda x:(m[x],m[x+1]), range(0,len(m)-2,2))

which results in:

[('a', 'b'), ('c', 'd'), ('e', 'f')]

You'll have to make sure that your original list has an even number of
items.


Happy zoping!

	Jo.


-- 
Internetmanufaktur Jo Meder ---------------------- Berlin, Germany
http://www.meder.de/ ------------------- fon: ++49-30-417 17 63 33
Kollwitzstr. 75 ------------------------ fax: ++49-30-417 17 63 45
10435 Berlin --------------------------- mob: ++49-170- 2 98 89 97 
Public GnuPG-Key ---------- http://www.meder.de/keys/jo-pubkey.txt