merge list of tuples with list

MRAB python at mrabarnett.plus.com
Tue Oct 19 22:07:23 EDT 2010


On 20/10/2010 02:26, Paul Rubin wrote:
> Daniel Wagner<brocki2301 at googlemail.com>  writes:
>>>> My short question: I'm searching for a nice way to merge a list of
>>>> tuples with another tuple or list. Short example:
>>>> a = [(1,2,3), (4,5,6)]
>>>> b = (7,8) ...
>> the output should look like:
>> a = [(1,2,3,7), (4,5,6,8)]
>
> That is not really in the spirit of tuples, which are basically supposed
> to be of fixed size (like C structs).  But you could write:
>
>    >>>  [x+(y,) for x,y in zip(a,b)]
>    [(1, 2, 3, 7), (4, 5, 6, 8)]

In Python 2.x:

     zip(*zip(*a) + [b])

In Python 3.x:

     list(zip(*list(zip(*a)) + [b]))



More information about the Python-list mailing list