Don’t Get Caught by Reference! Learn How to Copy Lists in Python

…"

Updated August 26, 2023



This tutorial dives deep into the concept of list copying in Python, explaining why it’s crucial and how to do it correctly. We’ll explore common pitfalls beginners face and provide clear examples for efficient and readable code.

Let’s imagine you have a basket of apples. You want to share some with a friend but also keep some for yourself. Simply handing over the entire basket wouldn’t be ideal, right? Your friend might eat all the apples, leaving you with nothing!

Similarly in Python, when dealing with lists (which are like our baskets), simply assigning one list to another doesn’t create a separate copy. It creates a reference - both variables point to the same list in memory. Any changes made through one variable will affect the other.

Why is Copying Important?

Copying lists allows you to work with independent data structures. This prevents unintended side effects when modifying list elements. Let’s illustrate:

original_list = [1, 2, 3]
copied_list = original_list  # Just a reference, not a copy!

copied_list.append(4) 
print(original_list)  # Output: [1, 2, 3, 4] - Oops! The original list changed too!

As you can see, appending ‘4’ to copied_list modified original_list. This is because they both refer to the same underlying list.

Creating True Copies:

To create a true copy where modifications in one list don’t affect the other, Python offers two primary methods:

  1. Slicing: This creates a shallow copy by extracting all elements from the original list into a new list.

    original_list = [1, 2, 3]
    copied_list = original_list[:] # Slice the entire list
    
    copied_list.append(4)
    print(original_list)  # Output: [1, 2, 3] - The original list remains unchanged!
    
  2. copy() method: This method is available for lists and creates a shallow copy efficiently.

    original_list = [1, 2, 3]
    copied_list = original_list.copy()
    
    copied_list.append(4)
    print(original_list)  # Output: [1, 2, 3] - The original list remains unchanged!
    

Shallow vs Deep Copies:

What happens if your list contains nested lists (lists within lists)? Shallow copies only duplicate the top-level list. The nested lists themselves still share references. To create a completely independent copy of all levels, you’d need a “deep copy.”

The copy module provides the deepcopy() function for this purpose:

import copy

original_list = [[1, 2], [3, 4]]
copied_list = copy.deepcopy(original_list)

copied_list[0][0] = 9  # Modify a nested element in the copied list

print(original_list) # Output: [[1, 2], [3, 4]] - Original list unchanged!
print(copied_list)   # Output: [[9, 2], [3, 4]] - Changes only in the copied list

Common Mistakes:

  • Assuming assignment creates a copy: Remember, new_list = old_list only creates a reference.

  • Forgetting deep copies for nested lists: Use copy.deepcopy() when you need independent copies at all levels.

Tips for Efficiency and Readability:

  • Choose the appropriate copying method based on your needs: slicing is often faster for simple shallow copies, while copy() is convenient for general use.
  • Use meaningful variable names to clearly indicate whether a variable holds a copy or a reference.

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

Intuit Mailchimp