Cartesian product of two lists in python
JavaScript
x
3
1
list1 = ['a', 'b']
2
list2 = [1, 2, 3, 4, 5]
3
Expected Output:
JavaScript
1
2
1
list3 = ['a1', 'a2', 'a3', 'a4', 'a5', 'b1', 'b2', 'b3', 'b4', 'b5']
2
Advertisement
Answer
Do a list comprehension, iterate over both the lists and add the strings, like
JavaScript
1
2
1
list3 = [i+str(j) for i in list1 for j in list2]
2