How do I concatenate two lists in Python?

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

    You could simply use the + or += operator as follows:

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

    Or:

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

    Also, if you want the values in the merged list to be unique you can do:

    c = list(set(a + b))
    

提交回复
热议问题