I have two lists and you want to combine them into one tuples list. I want to do this with list comprehension , I can make it work with map . but it would be nice to know how it would look here. code here
>>> lst = [1,2,3,4,5] >>> lst2 = [6,7,8,9,10] >>> tup = map(None,lst,lst2) # works fine >>> tup [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)] >>> l3 = [lst, lst2] >>> l3 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] >>> zip(*l3) # works fine [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)] >>> [(i,j) for i in lst and for j in lst2] # does not work File "<stdin>", line 1 [(i,j) for i in lst and for j in lst2] ^ SyntaxError: invalid syntax >>>
I wrote comments where it works and where not. How can I link two for-loop in list comprehension
python list tuples list-comprehension
eagertoLearn
source share