copy() – copy list

Overview

The copy() method in Python is a built-in list method used to create a shallow copy of a list. This method is useful when you need to duplicate a list, ensuring that the new list is a separate object but retains the same elements as the original list. A shallow copy means that the new list contains references to the same elements as the original list, rather than creating copies of the elements themselves.

Syntax

The syntax for the copy() method is straightforward:

new_list = original_list.copy()
  • original_list: The list you want to copy.
  • new_list: The new list that will be a shallow copy of original_list.

Basic Usage

Using the copy() method, you can create a shallow copy of a list. Here is an example:

# Create a list with some elements
fruits = ["apple", "banana", "cherry", "orange"]

# Create a copy of the list
fruits_copy = fruits.copy()

# Print the original and copied lists
print(fruits)       # Output: ["apple", "banana", "cherry", "orange"]
print(fruits_copy)  # Output: ["apple", "banana", "cherry", "orange"]

Shallow Copy vs. Deep Copy

A shallow copy, created using the copy() method, means that the new list contains references to the same objects as the original list. Changes to mutable elements within these objects will affect both lists. In contrast, a deep copy creates a new list with copies of the objects found in the original list, so changes to elements in the copied list do not affect the original list.

Example of Shallow Copy

# Create a list with some elements
fruits = ["apple", "banana", "cherry", "orange"]

# Create a copy of the list
fruits_copy = fruits.copy()

# Print the original and copied lists
print(fruits)       # Output: ["apple", "banana", "cherry", "orange"]
print(fruits_copy)  # Output: ["apple", "banana", "cherry", "orange"]

In this example, modifying an element in the original list affects the shallow copy because both lists reference the same nested objects.

Creating a Deep Copy

To create a deep copy, you can use the copy module’s deepcopy() function. This ensures that all objects within the list are recursively copied.

import copy

# Create a list with nested elements
nested_list = [[1, 2, 3], ["a", "b", "c"]]

# Create a deep copy of the list
deep_copy = copy.deepcopy(nested_list)

# Modify an element in the original list
nested_list[0][0] = 10

# Print the lists to see the effect
print(nested_list)     # Output: [[10, 2, 3], ["a", "b", "c"]]
print(deep_copy)       # Output: [[1, 2, 3], ["a", "b", "c"]]

Comparison with Other Methods

While the copy() method is a convenient way to create a shallow copy of a list, there are other methods to achieve similar results:

Using Slice Notation

You can create a shallow copy of a list using slice notation:

# Create a list
fruits = ["apple", "banana", "cherry", "orange"]

# Create a copy of the list using slice notation
fruits_copy = fruits[:]

# Print the original and copied lists
print(fruits)       # Output: ["apple", "banana", "cherry", "orange"]
print(fruits_copy)  # Output: ["apple", "banana", "cherry", "orange"]

Using the list() Constructor

Another way to create a shallow copy is by using the list() constructor:

# Create a list
fruits = ["apple", "banana", "cherry", "orange"]

# Create a copy of the list using the list constructor
fruits_copy = list(fruits)

# Print the original and copied lists
print(fruits)       # Output: ["apple", "banana", "cherry", "orange"]
print(fruits_copy)  # Output: ["apple", "banana", "cherry", "orange"]

Performance Considerations

Creating a shallow copy with the copy() method is generally efficient, with a time complexity of O(n), where n is the number of elements in the list. This is because the method needs to iterate through the list and copy the references to the elements.

Conclusion

The copy() method is a useful tool for creating shallow copies of lists in Python. It provides a simple and readable way to duplicate a list while retaining references to the same elements.