Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip list of tuples with flat list

I'm wondering if there's an easy way to do the following in Python 3.x. Say I have two lists structured as follows:

list_a = [(1,2), (1,2), (1,2), ...]
list_b = [3, 3, 3, ...]

What's the simplest way to produce a generator (here represented by calling a function funky_zip) that would let me iterate through these two lists like so:

>>> for a, b, c, in funky_zip(list_a, list_b):
>>>      print(a, b, c)
...
1 2 3
1 2 3
1 2 3
# and so on

I could just do

for aa, b in zip(list_a, list_b):
    print(aa[0], aa[1], b)

but I'm wondering if there's a nice way to do this without having to unpack the tuples. Thanks!

like image 334
mostsquares Avatar asked May 01 '16 00:05

mostsquares


1 Answers

You just need parentheses:

list_a = [(1,2), (1,2), (1,2)]
list_b = [3, 3, 3]
for (a, b), c in zip(list_a, list_b):
    print(a, b, c)

Result:

1 2 3
1 2 3
1 2 3
like image 196
TigerhawkT3 Avatar answered Sep 28 '22 10:09

TigerhawkT3