Python – In Python, is there a concise way to use a list comprehension with multiple iterators


Basically, I would like to build a list comprehension over the "cartesian product" of two iterators. Think about the following Haskell code:

[(i,j) | i <- [1,2], j <- [1..4]]

which yields


Can I obtain a similar behavior in Python in a concise way?

Best Solution

Are you asking about this?

[ (i,j) for i in range(1,3) for j in range(1,5) ]
Related Question