Home > Net >  Getting two lists into a list of lists in a orderly fashion
Getting two lists into a list of lists in a orderly fashion

Time:10-03

  • What I have

  • l1 = [1,2,3,4,5]

  • l2 = [6,7,8,9,10]

  • What i want:

  • l3 = [ [1,6], [2,7], [3,8], [4,9], [5,10] ]

CodePudding user response:

Assuming both lists have the same length:

l1 = [1,2,3,4,5]
l2 = [6,7,8,9,10]
output = [[l1[i], l2[i]] for i in range(0, len(l1))]
print(output)  # [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]

CodePudding user response:

Use zip:

zipped = zip(l1, l2)
result = [ x for x in zipped ]

# Result:
# [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
  • Related