Interleave multiple lists of the same length in Python

后端 未结 9 1585
刺人心
刺人心 2020-11-22 10:11

In Python, is there a good way to interleave two lists of the same length?

Say I\'m given [1,2,3] and [10,20,30]. I\'d like to transform th

9条回答
  •  眼角桃花
    2020-11-22 10:31

    Too late to the party, and there is plenty of good answers but I would also like to provide a simple solution using extend() method:

    list1 = [1, 2, 3]
    list2 = [10, 20, 30]
    
    new_list = []
    for i in range(len(list1)):
        new_list.extend([list1[i], list2[i]])
    print(new_list)
    

    Output:

    [1, 10, 2, 20, 3, 30]
    

提交回复
热议问题