opposite of zip()?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Dec 15 02:17:37 EST 2007


On Sat, 15 Dec 2007 06:46:44 +0000, Steven D'Aprano wrote:

> Here's how *not* to use it to do what you want:
> 
>>>> arrays = [[1, 2, 3, 4], [101, 102, 103, 104]] tupl = tuple("ab")
>>>> map(lambda alist, x: alist.append(x), arrays, tupl)
> [None, None]
>>>> arrays
> [[1, 2, 3, 4, 'a'], [101, 102, 103, 104, 'b']]
> 
> It works, but is confusing and hard to understand, and the lambda
> probably makes it slow. Don't do it that way.

As Gary Herron points out, you don't need to use lambda:

map(list.append, arrays, tupl)

will work. I still maintain that this is the wrong way to to it: taking 
the lambda out makes the map() based solution marginally faster than the 
explicit loop, but I don't believe that the gain in speed is worth the 
loss in readability.

(e.g. on my PC, for an array of 900000 sub-lists, the map() version takes 
0.4 second versus 0.5 second for the explicit loop. For smaller arrays, 
the results are similar.)



-- 
Steven.



More information about the Python-list mailing list