-1

I have two lists of the same length. I want to merge them into a list of lists like this example:

left_list = [-1,-3,15,3,1.7]
right_list = [1.2,2,17,3.5,2]

res_list = [[-1,1.2],[-3,2],[15,17],[3,3.5],[1.7,2]]

notice the left_list has smaller valleues so the order matters. Thanks!!

Programming Noob
  • 1,232
  • 3
  • 14

1 Answers1

0

pop the items from each list then form tuples and append them to a list

left_list = [-1,-3,15,3,1.7]
right_list = [1.2,2,17,3.5,2,4]

new_list=[]
while left_list or right_list:
    
    if len(left_list)>0:
        item1=left_list.pop(0)
    else:
        item1=np.nan
    if len(right_list)>0:
        item2=right_list.pop(0)
    else:
        item2=np.nan
    new_list.append((item1,item2))
    
print(new_list) 
Golden Lion
  • 3,840
  • 2
  • 26
  • 35