0

How to get all mapping from input array to another array. Some thing like below ?

a = ("a", "b", "c") b = ("d", "e")

Expected output

a-d
a-e
b-d
b-e
c-d
c-e

My current approach

a = ("a", "b", "c")
b = ("d", "e")

x = zip(a, b)
# [('a', 'd'), ('b', 'e')] 

Current solution

for i in a:
   for k in b:
      print(i, k)

Any Advance API or faster method than current approach in python ?

ajayramesh
  • 3,576
  • 8
  • 50
  • 75

1 Answers1

4

Use itertools.product

I.e., itertools.product(a, b)

itertools is part of the standard library

Code Snippet

import itertools

a = ("a", "b", "c")
b = ("d", "e")

list(itertools.product(a, b))
ajayramesh
  • 3,576
  • 8
  • 50
  • 75
Quentin
  • 700
  • 4
  • 10