How do I concatenate two lists in Python?

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

    Use a simple list comprehension:

    joined_list = [item for list_ in [list_one, list_two] for item in list_]
    

    It has all the advantages of the newest approach of using Additional Unpacking Generalizations - i.e. you can concatenate an arbitrary number of different iterables (for example, lists, tuples, ranges, and generators) that way - and it's not limited to Python 3.5 or later.

提交回复
热议问题