-1

current

list1 = ['A','B','C']
list2 = [1,2,3]

Desired

list 3 = [['A', 1], ['A', 2], ['A', 3], ['B', 1], ['B', 2],['B', 3],
 ['C', 1], ['C', 2], ['C', 3]]

What I have tried

list3 = [l+str(n) for l in list1 for n in list2]

results in:

['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']
Christian Torres
  • 143
  • 1
  • 1
  • 7

3 Answers3

1

You were close:

[[l,n] for l in list1 for n in list2]
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0
[[l]+[n] for l in list1 for n in list2]
-1

As Mark has already mentioned, itertools.product would be the way to go. But if you want to continue to use a list comprehension, the correct line would be

list3 = [ [l, str(n)] for l in list1 for n in list2 ]
Koustav Samaddar
  • 51
  • 1
  • 1
  • 6