1

Let's assume that I have the following three lists (l1, l2, l3). How can I create a new list where each element is a tuple of elements of the lists (l_desired)? It actually works as an extended version of the zip method in python.

In simpler words, given l1, l2, l3, how can I create l_desired?

l1 = [1,2,3]
l2 = ['a', 'b', 'c']
l3 = ['Jess', 'Muss']

l_desiered = [(1, 'a', 'Jess'), (1, 'b', 'Jess'), (1, 'c', 'Jess'), 
              (1, 'a', 'Muss'), (1, 'b', 'Muss'), (1, 'c', 'Muss'), 
              (2, 'a', 'Jess'), (2, 'b', 'Jess'), (2, 'c', 'Jess'), ...]

`

Redfox-Codder
  • 183
  • 2
  • 9
  • Are you talking about the cartesian product? What is the expected size? If so, `[(x,y,z) for x in l1 for y in l2 for z in l3]` – juanpa.arrivillaga Apr 23 '21 at 06:14
  • 1
    Does this answer your question? [Get the cartesian product of a series of lists?](https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists) – Aaron Apr 23 '21 at 06:15

6 Answers6

3

As others have already said, use itertools.product():

>>> l1 = [1,2,3]
>>> l2 = ['a', 'b', 'c']
>>> l3 = ['Jess', 'Muss']
>>> list(itertools.product(l1, l2, l3))
[(1, 'a', 'Jess'), (1, 'a', 'Muss'), (1, 'b', 'Jess'), (1, 'b', 'Muss'), (1, 'c', 'Jess'), (1, 'c', 'Muss'), (2, 'a', 'Jess'), (2, 'a', 'Muss'), (2, 'b', 'Jess'), (2, 'b', 'Muss'), (2, 'c', 'Jess'), (2, 'c', 'Muss'), (3, 'a', 'Jess'), (3, 'a', 'Muss'), (3, 'b', 'Jess'), (3, 'b', 'Muss'), (3, 'c', 'Jess'), (3, 'c', 'Muss')]

To achieve the sort order specified in your question, you can sort the results like this:

>>> from operator import itemgetter
>>> sorted(itertools.product(l1, l2, l3), key=itemgetter(0,2,1))
[(1, 'a', 'Jess'), (1, 'b', 'Jess'), (1, 'c', 'Jess'), (1, 'a', 'Muss'), (1, 'b', 'Muss'), (1, 'c', 'Muss'), (2, 'a', 'Jess'), (2, 'b', 'Jess'), (2, 'c', 'Jess'), (2, 'a', 'Muss'), (2, 'b', 'Muss'), (2, 'c', 'Muss'), (3, 'a', 'Jess'), (3, 'b', 'Jess'), (3, 'c', 'Jess'), (3, 'a', 'Muss'), (3, 'b', 'Muss'), (3, 'c', 'Muss')]
mhawke
  • 84,695
  • 9
  • 117
  • 138
1

Use itertools.product to get the Cartesian product (every possible combination of values) from a group of lists

import itertools
l1 = [1, 2, 3]
l2 = ['a', 'b', 'c']
l3 = ['Jess', 'Muss']
for element in itertools.product(l1, l2, l3):
    print(element)

Or you can use list comprehension

[element for element in itertools.product(l1, l2, l3)]

Or try this,

list(itertools.product(l1, l2, l3))
mhhabib
  • 2,975
  • 1
  • 15
  • 29
1

This will produce output identical to l_desired if that is important to you, otherwise the itertools.product solutions are tidier.

Example:

from pprint import pprint

l1 = [1,2,3]
l2 = ['a', 'b', 'c']
l3 = ['Jess', 'Muss']

l_desired = [
    (l1_elem, l2_elem, l3_elem)
    for l1_elem in l1
    for l3_elem in l3
    for l2_elem in l2
]

pprint(l_desired)

Output:

[(1, 'a', 'Jess'),
 (1, 'b', 'Jess'),
 (1, 'c', 'Jess'),
 (1, 'a', 'Muss'),
 (1, 'b', 'Muss'),
 (1, 'c', 'Muss'),
 (2, 'a', 'Jess'),
 (2, 'b', 'Jess'),
 (2, 'c', 'Jess'),
 (2, 'a', 'Muss'),
 (2, 'b', 'Muss'),
 (2, 'c', 'Muss'),
 (3, 'a', 'Jess'),
 (3, 'b', 'Jess'),
 (3, 'c', 'Jess'),
 (3, 'a', 'Muss'),
 (3, 'b', 'Muss'),
 (3, 'c', 'Muss')]
rhurwitz
  • 2,557
  • 2
  • 10
  • 18
0

If the order does not matter you can use itertools:

from itertools import product


l1 = [1,2,3]
l2 = ['a', 'b', 'c']
l3 = ['Jess', 'Muss']
l_desired = list(product(l1,l2,l3))

output:

[(1, 'a', 'Jess'), (1, 'a', 'Muss'), (1, 'b', 'Jess'), (1, 'b', 'Muss'), (1, 'c', 'Jess'), (1, 'c', 'Muss'), (2, 'a', 'Jess'), (2, 'a', 'Muss'), (2, 'b', 'Jess'), (2, 'b', 'Muss'), (2, 'c', 'Jess'), (2, 'c', 'Muss'), (3, 'a', 'Jess'), (3, 'a', 'Muss'), (3, 'b', 'Jess'), (3, 'b', 'Muss'), (3, 'c', 'Jess'), (3, 'c', 'Muss')]
joostblack
  • 2,465
  • 5
  • 14
0

You can also simply do this without importing itertools just by list comprehension:

l1 = [1,2,3]
l2 = ['a', 'b', 'c']
l3 = ['Jess', 'Muss']

Finally:

l_desiered=list(((x,y,z) for x in l1 for y in l2 for z in l3))

Now If you print l_desired you will get your desired output

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
0

Nested for loops:

l1 = [1,2,3]
l2 = ['a', 'b', 'c']
l3 = ['Jess', 'Muss']
out = []
for l in l1:
    for l_2 in l2:
        for l_3 in l3:
            out.append((l, l_2, l_3))
print(out)
'''
[
(1, 'a', 'Jess'), (1, 'a', 'Muss'), (1, 'b', 'Jess'),
(1, 'b', 'Muss'), (1, 'c', 'Jess'), (1, 'c', 'Muss'), 
(2, 'a', 'Jess'), (2, 'a', 'Muss'), (2, 'b', 'Jess'), 
(2, 'b', 'Muss'), (2, 'c', 'Jess'), (2, 'c', 'Muss'), 
(3, 'a', 'Jess'), (3, 'a', 'Muss'), (3, 'b', 'Jess'), 
(3, 'b', 'Muss'), (3, 'c', 'Jess'), (3, 'c', 'Muss')
]'''

Sid
  • 2,174
  • 1
  • 13
  • 29