In one line, how can I generate a list which contains the tuples of all pairs of l1 x l2.
Example: [1,2] et ['a','b'] -> [(1,'a'), (1,'b'), (2,'a'), (2,'b')]
I tried to use map() and zip() but I haven't found the answer yet.
In one line, how can I generate a list which contains the tuples of all pairs of l1 x l2.
Example: [1,2] et ['a','b'] -> [(1,'a'), (1,'b'), (2,'a'), (2,'b')]
I tried to use map() and zip() but I haven't found the answer yet.
you can use itertools.product
from itertools import product
numbers = [1,2]
letters = ['a','b']
result = list(product(numbers,letters))
It's as simple as this. Below is an example of a double iteration in listcomprehension.
numbers = [1,2]
letters = ['a','b']
new = [(num,letter) for num in numbers for letter in letters]
output
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]