Don’t Modify the Original! Learn How to Copy Lists Safely in Python

This tutorial dives into the nuances of copying lists in Python, explaining why a simple assignment won’t do and introducing the concept of shallow copies. …

Updated August 26, 2023



This tutorial dives into the nuances of copying lists in Python, explaining why a simple assignment won’t do and introducing the concept of shallow copies.

In Python, lists are incredibly versatile data structures used to store ordered collections of items. Imagine them as containers that hold anything from numbers and strings to even other lists!

Now, let’s say you want to create a duplicate of a list so you can work with it independently without altering the original. Seems straightforward, right? Just assign the original list to a new variable? Not quite!

The Problem: References

In Python, when you assign a list to a new variable using a simple assignment (=), you’re not creating a brand-new copy. Instead, you’re creating another reference pointing to the same list in memory. Think of it like two people holding onto the same map – changes made by one person will be reflected on the other’s map as well.

Let’s illustrate:

original_list = [1, 2, 3]
copied_list = original_list

copied_list.append(4)
print(original_list)  # Output: [1, 2, 3, 4]
print(copied_list)   # Output: [1, 2, 3, 4]

See what happened? Modifying copied_list also changed original_list. This is because they both refer to the same list object in memory.

The Solution: Shallow Copies

To create a true independent copy of a list, we need to employ shallow copying. This process creates a new list object containing references to the same elements as the original list. However, if those elements are themselves mutable objects (like other lists!), changes made within nested lists will still affect both copies.

Here are two common methods for shallow copying:

  1. Using the list() Constructor:

    original_list = [1, 2, [3, 4]]
    copied_list = list(original_list)
    
    copied_list[2].append(5)  # Modifies the nested list
    
    print(original_list)  # Output: [1, 2, [3, 4, 5]]
    print(copied_list)    # Output: [1, 2, [3, 4, 5]]
    
  2. Using the copy Module:

    import copy
    
    original_list = [1, 2, [3, 4]]
    copied_list = copy.copy(original_list)
    
    copied_list[2].append(5)  # Modifies the nested list
    
    print(original_list)  # Output: [1, 2, [3, 4, 5]]
    print(copied_list)    # Output: [1, 2, [3, 4, 5]]
    

Important Considerations:

  • Shallow copies only work one level deep. If your list contains nested mutable objects, changes within those nested objects will be reflected in both the original and copied lists.

  • For a truly independent copy of a list with nested mutable objects, you’ll need to use deep copying, which involves recursively creating new copies of all nested objects. The copy.deepcopy() function can be used for this purpose.

Let me know if you’d like to delve into deep copying in more detail!


Stay up to date on the latest in Computer Vision and AI

Intuit Mailchimp