How do I concatenate two lists in Python?

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

    It's also possible to create a generator that simply iterates over the items in both lists using itertools.chain(). This allows you to chain lists (or any iterable) together for processing without copying the items to a new list:

    import itertools
    for item in itertools.chain(listone, listtwo):
        # Do something with each list item
    

提交回复
热议问题