Built with Tutify.io

    AIStudyLabs

    Append List to Another List in Python

    Append List to Another List in Python

    How to append a list to another list in Python? To append one list to another list in Python you can use the extend(), concatenation + operator, for loop, slicing, * unpacking, and itertools.chain() functions. In this article, I will explain how to append one list to another list by using all these methods with examples.

    1. Quick Examples of Append List to Another List

    If you are in a hurry, below are some quick examples of how to append a list to another list.

    
    # Quick examples of append list to another list
    
    # Example 1: Append list into another list 
    # Using extend() method
    list1 = [2, 4, 6]  
    list2 = [8, 10, 12]  
    list1.extend(list2) 
    
    # Example 2: Append a list to another list
    # keeping a copy of original list
    list1 = [1, 3, 5]
    list2 = [7, 9, 11]
    result = list1.copy()
    result.extend(list2)
    
    # Example 3: Use chain() function
    # itertools module
    list1 = [1, 3, 5]
    list2 = [7, 9, 11]
    list3 = [13, 15, 17]
    result = list(itertools.chain(list1, list2, list3))
    
    # Example 4: Append a list to another list
    # Using For Loop
    list1 = [2, 4, 6, 8]
    list2 = [10, 12, 14, 16]
    for element in list2:
        list1.append(element)
    
    # Example 5: Use concatenation + operator 
    # To append multiple lists
    list1 = [0, 2, 4]
    list2 = [6, 8, 10]
    list3 = [12, 14, 16]
    result = list1 + list2 + list3
    
    # Example 6: Using slicing assignment
    list1 = [0, 2, 4]
    list2 = [6, 8, 10]
    list1[len(list1):] = list2
    
    # Example 7: Unpacking generalizations
    # Unpack both lists in a list literal
    list1 = [0, 1, 2]
    list2 = [3, 4, 5]
    result = [*list1, *list2]
    

    <a id="h-2-append-list-into-another-list-using-extend-method"></a>

    2. Append List Into Another List Using extend() Method

    The extend() method in Python allows you to append elements of an iterable (such as a list, tuple, or string) to the end of another list. This will append the elements of the list to the end of the existing list. By using this you can also append multiple elements to the list.

    
    # Append list into another list 
    # Using extend() method
    list1 = [2, 4, 6]  
    list2 = [8, 10, 12]  
    list1.extend(list2) 
    print(list1)
    
    # Using extend() method
    list1 = [2, 4, 6]    
    list1.extend([8, 10, 12]) 
    print(list1)
    
    # Output:
    # [2, 4, 6, 8, 10, 12]
    

    Alternatively, you can use append a list to another list while keeping a copy of the original list. For example, the copy() method is used to create a new list result that contains the same elements as list1. The extend() method is then used to add the elements of list2 to the result.

    
    # Append a list to another list
    # keeping a copy of original list
    list1 = [1, 3, 5]
    list2 = [7, 9, 11]
    result = list1.copy()
    result.extend(list2)
    print(result)
    
    # Output:
    # [1, 3, 5, 7, 9, 11]
    

    <a id="h-3-append-list-into-another-list-using-itertools-chain"></a>

    3. Append List Into Another List Using itertools.chain()

    Theitertoolsmodule in Python provides fast and memory-efficient utility functions for working with tables, such as lists, tuples, and generators. The chain() function in this module allows you to concatenate multiple same-type iterable into a single iterable, which can then be used to create a list or other type of iterable.

    For example, the chain() function is used to concatenate list1, list2, and list3 into a single iterable, which is then converted to a list using the list() function. The resulting list contains all the elements of the input lists, in order.

    
    import itertools
    # Use chain() function
    # Itertools module
    list1 = [1, 3, 5]
    list2 = [7, 9, 11]
    list3 = [13, 15, 17]
    result = list(itertools.chain(list1, list2, list3))
    print(result)
    
    # Output:
    # [1, 3, 5, 7, 9, 11, 13, 15, 17]
    

    4. Using For Loop

    You can use a for loop to iterate over the elements of a second list and append each of these elements to the first list using the append() function. For example, the for loop iterates over the elements of list2, and the append() function is called on list1 to append each element of list2 to the end of list1.

    
    # Append a list to another list
    # Using For Loop
    list1 = [2, 4, 6, 8]
    list2 = [10, 12, 14, 16]
    
    for element in list2:
        list1.append(element)
    print(list1)
    
    # Output:
    # [2, 4, 6, 8, 10, 12, 14, 16]
    

    <a id="h-5-use-concatenation-operator-to-append-multiple-lists"></a>

    5. Use Concatenation + Operator to Append Multiple Lists

    Similarly, to append multiple lists use the concatenation "+" operator. You can use the "+" operator to join the lists together in the order you want them to appear in the new concatenated list. For example,

    
    # Use concatenation + operator 
    # To append multiple lists
    list1 = [0, 2, 4]
    list2 = [6, 8, 10]
    list3 = [12, 14, 16]
    result = list1 + list2 + list3
    print(result)
    
    # Output:
    # [0, 2, 4, 6, 8, 10, 12, 14, 16]
    

    6. Append a list to another list Using slicing

    You can also use list slicing to append one list to another list in Python. For example,

    
    # Using slicing assignment
    list1 = [0, 2, 4]
    list2 = [6, 8, 10]
    list1[len(list1):] = list2
    print(list1)
    
    # Output:
    # [0, 2, 4, 6, 8, 10]
    

    7. Using * Unpacking Generalizations

    The * operator can also be used for unpacking generalizations, which allows you to pass a variable number of arguments to a function, let's use this * operator to combine two or more lists.

    
    # Unpacking generalizations
    # Unpack both lists in a list literal
    list1 = [0, 1, 2]
    list2 = [3, 4, 5]
    result = [*list1, *list2]
    print(result)
    
    # Output:
    # [0, 1, 2, 3, 4, 5]
    

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to append a list to another list in python by using the concatenation + operator, extend(), for loop, slicing, * unpacking, and itertools.chain() function with examples.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles