0

I have 2 lists which I need to iterate through:

hits_idx2 = [ 0, 1, 2]
common_b = [ [835,1234,2345] , [223,544] , [423,1234]  ]

in order to produce the following output:

0, 835
0, 1234
0, 2345
1, 223
1, 544
2, 423
2, 1234

I created a nested loop using list comprehensions and a check to see if the element returned at the each iteration if the last element in each sublist, if it not the loop should continue, if it is it should go to the next element of hits_idx2 and iterate through the second sublist in common_b:

for x,y in [(x,y) for x in hits_idx2 for ind in hits_idx2 for y in common_b[ind] if y!=common_b[ind][-1]]:
    print (x,y)

but unfortunately I am getting the following:

0 835
0 1234
0 223
0 423
1 835
1 1234
1 223
1 423
2 835
2 1234
2 223
2 423

I am kinda stuck, any help more than welcome. thanks!

I solved it as follows:

for h,y in [ (h,common) for h, common in [(h, common) for h, commons in zip(hits_idx2, common_b) for common in commons] ]:
    print(h,y)
Artemis
  • 123
  • 1
  • 8

0 Answers0