How do I concatenate two lists in Python?

前端 未结 25 1909
时光取名叫无心
时光取名叫无心 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 07:06

    I assume you are wanting one of the two methods:

    Keep Duplicate elements

    It is very easy, just concatenate like string:

    def concat_list(l1,l2):
        l3 = l1+l2
        return l3
    

    Next if you want to eliminate duplicate elements

    def concat_list(l1,l2):
       l3 = []
       for i in [l1,l2]:
         for j in i:   
           if j not in l3:   
             #Check if element exists in final list, if no then add element to list
             l3.append(j)
       return l3
    
    0 讨论(0)
提交回复
热议问题