Python – Copy Lists

In Python, there are several ways to copy a list, depending on whether you want to create a shallow copy or a deep copy. A shallow copy creates a new list, but the elements within the list still refer to the original objects (i.e., references to the same objects). A deep copy creates a new list and also recursively copies the objects inside the list, so the new list and the original list are completely independent.

1. Shallow Copy using copy()

The copy() method creates a shallow copy of the list, meaning that it creates a new list but does not copy the objects inside the list (they are still references to the same objects).

my_list = [1, 2, 3, 4, 5]

# Create a shallow copy of the list
new_list = my_list.copy()

print(new_list)  # Output: [1, 2, 3, 4, 5]

2. Shallow Copy using Slice ([:])

Another way to create a shallow copy of a list is by using the slice notation. The slice [:] copies the entire list.

my_list = [1, 2, 3, 4, 5]

# Create a shallow copy using slicing
new_list = my_list[:]

print(new_list)  # Output: [1, 2, 3, 4, 5]

3. Shallow Copy using list() Constructor

You can also use the list() constructor to create a shallow copy of a list.

my_list = [1, 2, 3, 4, 5]

# Create a shallow copy using the list() constructor
new_list = list(my_list)

print(new_list)  # Output: [1, 2, 3, 4, 5]

4. Deep Copy using copy Module

A deep copy creates a completely independent copy of the list, including copies of all the objects inside the list. You can achieve this using the deepcopy() function from the copy module.

import copy

my_list = [[1, 2], [3, 4], [5, 6]]

# Create a deep copy of the list
new_list = copy.deepcopy(my_list)

print(new_list)  # Output: [[1, 2], [3, 4], [5, 6]]

5. Shallow vs Deep Copy Example

To illustrate the difference between shallow and deep copies, let’s consider a list containing sublists.

Shallow Copy Example:

my_list = [[1, 2], [3, 4], [5, 6]]

# Create a shallow copy using copy()
new_list = my_list.copy()

# Modify the inner list in the original list
my_list[0][0] = 99

print("Original List:", my_list)  # Output: [[99, 2], [3, 4], [5, 6]]
print("Shallow Copied List:", new_list)  # Output: [[99, 2], [3, 4], [5, 6]]

In this case, modifying the inner list in the original list also affects the shallow copied list because the elements are references to the same objects.

Deep Copy Example:

my_list = [[1, 2], [3, 4], [5, 6]]

# Create a deep copy using deepcopy()
new_list = copy.deepcopy(my_list)

# Modify the inner list in the original list
my_list[0][0] = 99

print("Original List:", my_list)  # Output: [[99, 2], [3, 4], [5, 6]]
print("Deep Copied List:", new_list)  # Output: [[1, 2], [3, 4], [5, 6]]

In this case, the deep copied list remains unchanged, even though the original list was modified. This is because the deep copy recursively creates independent copies of the inner lists.

Summary:

  • Shallow Copy: Copies the list structure, but the elements are still references to the same objects.
    • Methods: .copy(), slicing ([:]), list().
  • Deep Copy: Copies the list structure and all elements (including nested objects), creating independent copies.
    • Method: copy.deepcopy().

You can choose between shallow and deep copies depending on your needs.

Leave a Reply 0

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