Packing list elements into tuples

Nickolay Kolev nmkolev at uni-bonn.de
Tue Nov 9 16:29:35 EST 2004


Hi all,

I have a list whose length is a multiple of 3. I want to get a list  of 
tuples each of which has 3 consecutive elements from the original list, 
thus packing the list into smaller packets. Like this:

l = [1,2,3,4,5,6]

tups = [(1,2,3), (4,5,6)]

or

l = [1,2,3,4,5,6,7,8,9]

tups = [(1,2,3), (4,5,6), (7,8,9)]

if i can dictionaries it would be even better:


l = [1,2,3,4,5,6]

tups = [
	{'first':1,'second':2,'third':3},
	{'first':4,'second':5,'third':6}
	]

and so on.

Any ideas?

Many thanks in advance!

Cheers,
   -- Nickolay



More information about the Python-list mailing list