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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With