Doggy Face
Doggy Face

Reputation: 409

How to combine 2 list into pairs of nested list?

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

Answers (2)

Teacher Mik
Teacher Mik

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

jezrael
jezrael

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

Related Questions