How do I concatenate two lists in Python?

前端 未结 25 2003
时光取名叫无心
时光取名叫无心 2020-11-21 06:18

How do I concatenate two lists in Python?

Example:

listone = [1, 2, 3]
listtwo = [4, 5, 6]

Expected outcome:

>&g         


        
25条回答
  •  眼角桃花
    2020-11-21 06:50

     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]
    

提交回复
热议问题