-7

I've these two lists a = [1, 2, 3] and another b = [4, 2, 1]

I want this outcome by join these list like this (items in a with every item of b) :

(1, 4) (1, 2) (1, 1)
(2, 4) (2, 2) (2, 1)
(3, 4) (3, 2) (3, 1)
Umar.H
  • 22,559
  • 7
  • 39
  • 74
  • 3
    You could use a nested loop. This seems like homework. What have you tried? Where are you stuck? – John Coleman Jan 20 '21 at 16:08
  • 2
    [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+python+tuples+cartesian+product+two+arrays) of [Get the cartesian product of a series of lists?](https://stackoverflow.com/q/533905/4642212). – Sebastian Simon Jan 20 '21 at 16:09

1 Answers1

1

For generating the cartesian product:

cart_prod = [(i, j) for i in a for j in b]

EDIT the question changed

>>> for i in a:
...   print([(i, j) for j in b])
...
[(1, 4), (1, 2), (1, 1)]
[(2, 4), (2, 2), (2, 1)]
[(3, 4), (3, 2), (3, 1)]
Filippo Vitale
  • 7,597
  • 3
  • 58
  • 64