opposite of zip()?

Gary Herron gherron at islandtraining.com
Sat Dec 15 01:09:12 EST 2007


igor.tatarinov at gmail.com wrote:
> Given a bunch of arrays, if I want to create tuples, there is
> zip(arrays). What if I want to do the opposite: break a tuple up and
> append the values to given arrays:
>    map(append, arrays, tupl)
> except there is no unbound append() (List.append() does not exist,
> right?).
>
> Without append(), I am forced to write a (slow) explicit loop:
>   for (a, v) in zip(arrays, tupl):
>       a.append(v)
>
> I assume using an index variable instead wouldn't be much faster.
>
> Is there a better solution?
>
> Thanks,
> igor
>   


But it *does* exist, and its named list.append, and it works as you wanted.

>>> list.append
<method 'append' of 'list' objects>
>>> a = [[],[]]
>>> map(list.append, a, (1,2))
[None, None]
>>> a
[[1], [2]]
>>> map(list.append, a, (3,4))
[None, None]
>>> a
[[1, 3], [2, 4]]
>>> map(list.append, a, (30,40))
[None, None]
>>> a
[[1, 3, 30], [2, 4, 40]]


Gary Herron





More information about the Python-list mailing list