How do I concatenate two lists in Python?

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

    You could also use the list.extend() method in order to add a list to the end of another one:

    listone = [1,2,3]
    listtwo = [4,5,6]
    
    listone.extend(listtwo)
    

    If you want to keep the original list intact, you can create a new list object, and extend both lists to it:

    mergedlist = []
    mergedlist.extend(listone)
    mergedlist.extend(listtwo)
    

提交回复
热议问题