What is the fastest way to merge two lists in python?

后端 未结 5 471
借酒劲吻你
借酒劲吻你 2020-12-10 10:49

Given,

list_1 = [1,2,3,4]
list_2 = [5,6,7,8]

What is the fastest way to achieve the following in python?

l         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-10 11:38

    a=[1,2,3]
    b=[4,5,6]
    
    c=a+b
    print(c)
    

    OUTPUT:

     >>> [1, 2, 3, 4, 5, 6]
    

    In the above code "+" operator is used to concatenate the 2 lists into a single list.

    ANOTHER SOLUTION:

     a=[1,2,3]
     b=[4,5,6]
     c=[] #Empty list in which we are going to append the values of list (a) and (b)
    
     for i in a:
         c.append(i)
     for j in b:
         c.append(j)
    
     print(c)
    

    OUTPUT:

    >>> [1, 2, 3, 4, 5, 6]
    

提交回复
热议问题