merge list of tuples with list

Daniel Wagner brocki2301 at googlemail.com
Wed Oct 20 23:32:53 CEST 2010


Many thanks for all these suggestions! here is a short proof that you
guys are absolutely right and my solution is pretty inefficient.

One of your ways:

$ python /[long_path]/timeit.py 'a=[(1,2,3),(4,5,6)];b=(7,8);[x+(y,)
for x,y in zip(a,b)]'
1000000 loops, best of 3: 1.44 usec per loop

And my way:

$ python /[long_path]/timeit.py 'a=[(1,2,3),
(4,5,6)];b=[7,8];map(tuple, map(lambda x: x + [b.pop(0)] , map(list,
a)))'
100000 loops, best of 3: 5.33 usec per loop

I really appreciate your solutions but they bring me to a new
question: Why is my solution so inefficient? The same operation
without the list/tuple conversion

$ python /[long_path]/timeit.py 'a=[[1,2,3],
[4,5,6]];b=[7,8];map(lambda x: x + [b.pop(0)] , a)'
100000 loops, best of 3: 3.36 usec per loop

is still horrible slow. Could anybody explain me what it makes so
slow? Is it the map() function or maybe the lambda construct?

Greetings,
Daniel



More information about the Python-list mailing list