[Solved] complex list comprehension syntax


You can generally turn a nested for loop that builds a list into a list comprehension by using the same exact for ... in statements in the same order:

list3 = [k for l in lst1 for k in lst2 if l == k[0]]
list4 = [k for l in lst1 for k in lst2 if l == k[1]]
list5 = [l for l in lst1 for k in lst2 if l not in k]

solved complex list comprehension syntax