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

iteratorlist-comprehensionpython

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

[(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4)]

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