Reputation: 409
So, I have 2 lists:
list1 = [1,2,3,4,5]
list2= [6,7,8,9,10]
The desired output:
newlist = [[1,6],[2,7],[3,8],[4,9],[5,10]]
How to do that in Python?
Upvotes: 1
Views: 184
Reputation: 176
Use the zip funciton
list1 = [1,2,3,4,5]
list2= [6,7,8,9,10]
list3 = list(zip(list1, list2))
print(list3)
Upvotes: 1
Reputation: 862611
Use zip
, but output is list of tuples, so added map
by list
:
a = list(map(list, zip(list1, list2)))
print (a)
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
Or use list comprehension:
a = [list(x) for x in zip(list1, list2)]
Upvotes: 1