Signup/Sign In

How to copy elements from one list to another in Python [Easy Way]

Posted in Programming   LAST UPDATED: MAY 26, 2023

    There are many ways to copy a list to another memory location. But are you aware of how these lists are copied under the hood?

    Just assigning the old list to a new empty list won't copy the content from the old list to the new list. When a list (which has elements) is assigned to a new list like, list_two = list_one, where list_two is a new empty list and list_one has a few elements, it doesn't really create two new lists with the same data. It just copies the reference from list_one to list_two, i.e list_two would contain the reference to the memory location to which list_one also refers to.

    To actually, truly copy content from list_one to list_two, many methods can be used, one of which is copy.deepcopy(), which we have covered in this tutorial.

    How to copy elements from one list to another in Python

    1. Using the built-in copy method

    This copy method is available in Python beginning from the Python 3.3 version. A shallow copy is made when the copy method is used. For example,

    list_two = []
    list_one = [1,2,4,4,7]
    list_two = list_one.copy()
    print(list_two)

    Output:

    [1, 2, 4, 4, 7]


    2. List slicing for copying contents from an old list to a new one

    Below is the code example to show how list slicing can be used to copy elements of one list to another list.

    list_two = []
    list_one = [1,2,4,4,7]
    list_two = list_one[:]
    print(list_two)

    Output:

    [1, 2, 4, 4, 7]

    3. Using the built-in list method

    The list method is used to create a new list.

    list_two = []
    list_one = [1,2,4,4,7]
    list_two = list(list_one)
    print(list_two)

    Output:

    [1, 2, 4, 4, 7]

    4. Using the generic copy.copy method

    This is comparatively slower than the built-in list method. This is because the generic copy method is required to find out the type of the list which has to be copied to the new list.

    import copy
    
    list_two = []
    list_one = [1,2,4,4,7]
    list_two = copy.copy(list_one)
    print(list_two)
    
    # if we change list_two, list_one is also changed
    list_two[1] = 500
    
    print("List two", list_two)
    print("List one", list_one)

    Output:

    [1, 2, 4, 4, 7]
    List two [1, 500, 4, 4, 7]
    List one [1, 500, 4, 4, 7]
    

    When we updated the list_two, the list_one also got changed because they both point to the same list in the memory. Hence, if one changes any list elements, those changes are reflected in the other list too.

    5. Using the copy.deepcopy method

    This method is used when we want a new copy of the list to be created. This method is extremely slow, but sometimes it has to be inevitably used as using this method a deep copy of list elements is created which means a separate copy of the list elements.

    In the code example below, when we copy the list a to list b, then the list elements are actually copied and a new list is created in the memory. While in all the other ways of copy that we covered above, only provide the reference of the existing list to the new list, not actually a copy of the list elements.

    import copy
    
    a = ['a', 'b', 1, 2, 3]
    b = copy.deepcopy(a)
    
    # if we change b elements, a is not changed
    b[2] = '10'
    
    print(a)
    print(b)

    Output:

    ['a', 'b', 1, 2, 3]
    ['a', 'b', '10', 2, 3]

    6. Using the * (unpacking) operator

    This operator is known as the unpacking operator although it has no official name. It was introduced in Python 3.5+.

    list_two = []
    list_one = [1,2,4,4,7]
    list_two = [*list_one]
    print(list_two)

    Output:

    [1, 2, 4, 4, 7]

    This works with simple lists only, not nested lists.

    Don't confuse the above code with the code below. The code below simply means multiply by 1, and it will repeat all the list elements of list_one once, if you change 1 with 2, the complete list will be repeated twice. This is not the unpacking operator, but using the * operator with 1, also means you are copying the list_one to list_two. It is similar to using a simple assignment operator i.e. list_one = list_two.

    list_two = []
    list_one = [1,2,4,4,7]
    list_two = list_one * 1
    print(list_two)
    list_two = list_one * 2
    print(list_two)

    Output:

    [1, 2, 4, 4, 7]
    [1, 2, 4, 4, 7, 1, 2, 4, 4, 7]
    

    Conclusion

    Copying elements from one list to another is a fundamental operation in Python, and mastering different copying techniques is crucial for efficient list manipulation. In this article, we explored various methods, including shallow copying, deep copying, and copying subsets of elements, providing you with a comprehensive understanding of list copying in Python.

    Now armed with this knowledge, you can confidently handle list operations, create backups, extract specific elements, and even modify elements during the copying process. Whether you're a beginner or an experienced Pythonista, these techniques will serve you well in your programming journey.

    Frequently Asked Questions(FAQs)

    1. In Python, can I transfer items from one list to another?
    Yes, you can transfer components from one list to another using techniques such as list slicing, list comprehension, and the built-in copy() function.


    2. Is a loop required to transfer items from one list to another?
    A: No, using a loop is not required. To duplicate components without using a loop, use list slicing or the built-in copy() method.

    3. Is it possible to transfer components between lists of varying lengths?
    Yes, you can copy components from a list of any length to another list, independent of the length of the target list.

    4. Will transferring components from one list to another cause the original to change?
    It depends on the method you use. Some methods, like list slicing, will not modify the original list, while others, like the built-in copy() method, will create a copy of the original list.

    5. How can I make a shallow copy of a list in Python?

    To make a shallow copy of a list, you can use the slicing technique: new_list = original_list[:] or the copy() method: new_list = original_list.copy().

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS