Lists aggregation

Armin feng.shaun at gmail.com
Sun Mar 15 19:15:54 CET 2009


On Monday 16 March 2009 15:07:06 mattia wrote:
> I have 2 lists, like:
> l1 = [1,2,3]
> l2 = [4,5]
> now I want to obtain a this new list:
> l = [(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)]
> Then I'll have to transform the values found in the new list.
> Now, some ideas (apart from the double loop to aggregate each element of
> l1 with each element of l2):
> - I wanted to use the zip function, but the new list will not aggregate
> (3,4) and (3,5)
> - Once I've the new list, I'll apply a map function (e.g. the exp of the
> values) to speed up the process
> Some help?
>
> Thanks, Mattia
> --
> http://mail.python.org/mailman/listinfo/python-list

zip wouldn't work here, you can use list comprehensions:

l = [(x, y) for x in l1 for y in l2]

-- 
Armin Moradi



More information about the Python-list mailing list