6

I have two different lists and I would like to know how I can get each element of one list print with each element of another list. I know I could use two for loops (each for one of the lists), however I want to use the zip() function because there's more that I will be doing in this for loop for which I will require parallel iteration.

I therefore attempted the following but the output is as shown below.

lasts = ['x', 'y', 'z']
firsts = ['a', 'b', 'c']

for last, first in zip(lasts, firsts):
    print (last, first, "\n")

Output:

x a 
y b 
z c 

Expected Output:

x a
x b
x c
y a
y b
y c
z a
z b
z c
5
  • Does this answer your question? Cartesian product of two lists in python Commented Feb 11, 2022 at 21:01
  • Also stackoverflow.com/q/533905/4046632 Commented Feb 11, 2022 at 21:01
  • As mentioned in my question, I need to use the zip() function. I checked other questions on this platform, but couldn't find one that utilizes the zip function to achieve the same goal Commented Feb 11, 2022 at 21:04
  • You may want to use zip, however it doesn't help in this case. itertools.product is the tool. You may want to elaborate further what you need zip for. Otherwise this is XY problem. Commented Feb 11, 2022 at 21:05
  • Could you elaborate more on the "parallel execution" part? I don't know about everyone else but I have troubles understanding why double loop doesn't work out for you. Commented Feb 11, 2022 at 21:09

3 Answers 3

8

I believe the function you are looking for is itertools.product:

lasts = ['x', 'y', 'z']
firsts = ['a', 'b', 'c']

from itertools import product
for last, first in product(lasts, firsts):
    print (last, first)

x a
x b
x c
y a
y b
y c
z a
z b
z c

Another alternative, that also produces an iterator is to use a nested comprehension:

iPairs=( (l,f) for l in lasts for f in firsts)
for last, first in iPairs:
    print (last, first)

If you must use zip(), both inputs must be extended to the total length (product of lengths) with the first one repeating items and the second one repeating the list:

iPairs = zip( (l for l in lasts for _ in firsts),
              (f for _ in lasts for f in firsts) )
Sign up to request clarification or add additional context in comments.

Comments

0

Honestly I think you wont be able to do it with zip because you are searching for another behaviour. To use syntactic sugar and make it work with zip will just void your debugging experience.

But if you would drag me by the knee:

zip([val for val in l1 for _ in range(len(l2))],
    [val for _ in l1 for val in l2])

where you first duplicate the first list to get xxxyyyzzz and duplicate the second list with abcabcabc

or

for last,first in [(l,f) for l in lasts for f in firsts]:
     print(last, first, "\n")

4 Comments

Basically, this zip with 2 list comprehensions only complicates the code instead of using 2 nesetd loops.
Thats true, but this is what he asked for without using any imports. Sure thing zip(itertools.product(l1,l2)) would be way better...
no, there is no need for zip in zip(itertools.product(l1,l2))
Sorry, yes thats true
-1

Honestly I think you wont be able to do it with zip because you are searching for another behaviour. To use syntactic sugar and make it work with zip will just void your debugging experience.

But if you would drag me by the knee:

zip([val for val in l1 for _ in range(len(l2))], [val for _ in l1 for val in l2])

1 Comment

Please do not copy answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.