How do I concatenate two lists in Python?

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

    import itertools
    
    A = list(zip([1,3,5,7,9],[2,4,6,8,10]))
    B = [1,3,5,7,9]+[2,4,6,8,10]
    C = list(set([1,3,5,7,9] + [2,4,6,8,10]))
    
    D = [1,3,5,7,9]
    D.append([2,4,6,8,10])
    
    E = [1,3,5,7,9]
    E.extend([2,4,6,8,10])
    
    F = []
    for a in itertools.chain([1,3,5,7,9], [2,4,6,8,10]):
        F.append(a)
    
    
    print ("A: " + str(A))
    print ("B: " + str(B))
    print ("C: " + str(C))
    print ("D: " + str(D))
    print ("E: " + str(E))
    print ("F: " + str(F))
    

    Output:

    A: [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
    B: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
    C: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    D: [1, 3, 5, 7, 9, [2, 4, 6, 8, 10]]
    E: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
    F: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
    

提交回复
热议问题