Unit 4 • Lesson 9

Copying and Comparing Data Structures

Overview

Learn how to clone lists, tuples, or dictionaries safely without unintended links between copies. This section explains shallow vs. deep copies and how equality works across structures, helping you avoid common bugs when working with mutable data.

Intermediate 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • Shallow copying: Understand how to create a copy that shares references.
  • Deep copying: Learn how to create completely independent copies.
  • Equality vs identity: Discover the difference between == and is.
  • Comparing structures: Understand how Python compares different data structures.

Why This Matters

When you assign a list to another variable, both variables point to the same list - changes to one affect the other! Understanding copying helps you avoid unexpected bugs. Knowing when to use == (equality) versus is (identity) helps you write correct comparisons!

Step 1: Assignment vs Copying

When you assign a list to another variable, both variables refer to the same object:

Assignment Creates a Reference
original = [1, 2, 3]
reference = original  # Both point to the same list!

reference.append(4)
print(original)  # Output: [1, 2, 3, 4] (both changed!)
print(reference)  # Output: [1, 2, 3, 4]
1

Same Object

Assignment (reference = original) doesn't create a new list - it creates a new name that points to the same list object in memory.

2

Shared Changes

When you modify one variable, the other variable sees the same changes because they're both pointing to the same object!

3

Need a Copy

If you want an independent copy that doesn't affect the original, you need to explicitly create a copy.

Key Concept: In Python, variables are like labels pointing to objects. When you assign reference = original, you're creating a new label for the same object, not creating a new object. To have a separate copy, you need to explicitly copy it!

Mini Practice #1: Understanding Assignment

Try It Yourself

See how assignment creates a reference:

Press Run to see output

What happened? Both list1 and list2 changed when you modified list2! This is because they both point to the same list object. Assignment doesn't create a copy - it creates a new name for the same object!

Step 2: Shallow Copying

A shallow copy creates a new object, but nested objects are still shared:

Creating Shallow Copies
original = [1, 2, 3]

# Method 1: Using copy()
shallow_copy = original.copy()

# Method 2: Using list()
shallow_copy = list(original)

# Method 3: Using slicing
shallow_copy = original[:]

shallow_copy.append(4)
print(original)  # Output: [1, 2, 3] (unchanged!)
print(shallow_copy)  # Output: [1, 2, 3, 4]

How It Works

A shallow copy creates a new list object, so changes to the copy don't affect the original. However, if the list contains nested lists or other mutable objects, those nested objects are still shared between the original and the copy!

Step 3: Deep Copying

A deep copy creates completely independent copies, including nested objects:

Creating Deep Copies
import copy

original = [[1, 2], [3, 4]]

# Shallow copy (nested lists still shared)
shallow = original.copy()
shallow[0].append(5)
print(original)  # Output: [[1, 2, 5], [3, 4]] (changed!)

# Deep copy (completely independent)
deep = copy.deepcopy(original)
deep[0].append(6)
print(original)  # Output: [[1, 2, 5], [3, 4]] (unchanged!)
print(deep)  # Output: [[1, 2, 5, 6], [3, 4]]

Remember: Use copy.deepcopy() when you need completely independent copies of nested structures. For simple lists without nested mutable objects, a shallow copy is usually sufficient!

Mini Practice #2: Creating Copies

Try It Yourself

Try creating copies:

Press Run to see output

What happened? The original list stayed unchanged when you modified the copy! This is because copy() created a new list object. The copy is independent of the original, so changes to the copy don't affect the original list!

Step 4: Equality vs Identity

Python has two ways to compare: == checks if values are equal, while is checks if they're the same object:

Equality vs Identity
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1

print(list1 == list2)  # Output: True (same values)
print(list1 is list2)  # Output: False (different objects)
print(list1 is list3)  # Output: True (same object)
1

Equality (==)

== checks if two objects have the same values. Two different lists with the same items are equal.

2

Identity (is)

is checks if two variables point to the same object in memory. This is stricter than equality!

3

When to Use

Use == to compare values. Use is to check if variables point to the same object (especially useful for checking None).

End-of-Lesson Exercises

Exercise 1: Create a Copy

Create a list called original with [1, 2, 3]. Create a copy called copy_list using the copy() method. Modify copy_list by appending 4, then print both lists to show they're independent.

Use copy() method to create a shallow copy.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Compare Equality and Identity

Create two lists: list1 = [1, 2, 3] and list2 = [1, 2, 3]. Check if they're equal using == and check if they're the same object using is. Print both results.

Use == for equality and is for identity checks.

Write your code above and click "Check Answer" to verify it's correct.