how to get tuples from lists using list comprehension in python -


i have 2 lists , want merges them 1 list of tuples. want list comprehension, can working using map. nice know how list comprehension here work. 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) in lst , j in lst2] # not work   file "<stdin>", line 1     [(i,j) in lst , j in lst2]                               ^ syntaxerror: invalid syntax >>>  

i have written comments works , not. how can 2 for-loop coupled in list comprehension

think list comprehensions loops. how can write 2 not nested loops?

you can wierd list comprehension:

[(x, lst2[i]) i, x in enumerate(lst)] 

or

[(lst[i], lst2[i]) in xrange(len(lst))] 

but actually, it's better use zip.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -