Make Copy of List With & Without Reference in Python (2 Examples)

In this tutorial, you’ll learn how to make a copy of a list in Python with and without reference. We’ll explore different methods to create copies of lists.

The table of contents is structured as follows:

Let’s get started!

Initializing a Sample List and Importing Modules

Let’s initialize a sample list that we’ll be working with throughout the tutorial. But, before doing that, we need to import the copy module to use its functions to copy our linked list.

import copy
 
# Initializing a list
original_list = [1, 2, 3, 4, 5]

This creates a list original_list with elements [1, 2, 3, 4, 5].

Example 1: Creating a Shallow Copy

The first example involves creating a shallow copy of the list. Here’s how it works:

# Shallow copy
shallow_copy = original_list
 
# Modify original list
original_list.append(6)
 
# Print original list
print(original_list)
# [1, 2, 3, 4, 5, 6]
 
# Print shallow copy
print(shallow_copy)
# [1, 2, 3, 4, 5, 6]

In this example, we create a shallow copy of the original list by assigning it to a new variable shallow_copy. Both variables point to the same memory location, so any modifications to one list will reflect in the other. When we append an element to the original list, it also affects the shallow copy.

Example 2: Creating a Deep Copy

The second example demonstrates creating a deep copy of the list. Here’s an example:

# Deep copy
deep_copy = copy.deepcopy(original_list)
 
# Modify original list
original_list.append(7)
 
# Print original list
print(original_list)
# [1, 2, 3, 4, 5, 6, 7]
 
# Print deep copy
print(deep_copy)
# [1, 2, 3, 4, 5, 6]

In this example, we use the copy module and its deepcopy() function to create a deep copy of the original list. The deep copy creates a new list with separate elements, so modifications to one list don’t affect the other. Even after appending an element to the original list, the deep copy remains unchanged.

Video, Further Resources & Summary

In this tutorial, we explored different methods to make a copy of a list in Python. We saw that creating a shallow copy using the assignment operator = results in both lists sharing the same memory location, while a deep copy using the copy.deepcopy() function creates a completely separate copy. It’s important to choose the appropriate method based on your requirements.

Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

Now you’re equipped with the knowledge to make copies of lists in Python!

 

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top