-1

I have the following input:

input = (['AA', 'BB', 'CC', 'DD'], '4100314')

I want output as ('AA','4100314'), ('BB','4100314'), ('CC','4100314'), ('DD','4100314')

zolo
  • 1
  • Have a look to [product](https://docs.python.org/3/library/itertools.html#itertools.product) – XtianP May 11 '21 at 18:34

1 Answers1

0

In your specific example this will work:

input1 = ['AA', 'BB', 'CC', 'DD']
input2 = '4100314'

def product(a, b):
    return list(map(lambda x: [x, b], a))

print(product(input1, input2))

Output:

[['AA', '4100314'], ['BB', '4100314'], ['CC', '4100314'], ['DD', '4100314']]

But I think this recommendation by @Ani is far better: Get the cartesian product of a series of lists?

An example using that method is as follows:

import itertools

input1 = ['AA', 'BB', 'CC', 'DD']
input2 = ['4100314']

def product(a, b):
    out = []
    for element in itertools.product(a, b):
        out.append(element)
    return out
        
print(product(input1, input2))

Output:

[('AA', '4100314'), ('BB', '4100314'), ('CC', '4100314'), ('DD', '4100314')]

The main difference here is that with the second method you can input any two arrays and it will output the correct result, whereas the first method will only work with an array and a single variable, as was your use case.

LaytonGB
  • 1,384
  • 1
  • 6
  • 20