In Python, the copy function is a method associated with lists that generates a shallow duplicate of a given list. This method produces a new list that contains identical elements to those found in the original list; nonetheless, it possesses its own distinct identity in memory. This functionality is particularly beneficial when it is important to ensure that modifications made to the new list do not influence the original list, and vice versa.
Syntax of the Python List copy Method
The syntax for the copy method of a Python list is as follows:
Syntax:
new_list = original_list.copy()
The copy function does not accept any arguments.
Return Value:
- The copy function generates a shallow duplicate of a list.
Examples of List copy
We will now examine a few instances of the list copy method in Python.
Example 1: Working of the copy Method
In the subsequent illustration, we will explore how the copy method operates to generate a shallow copy of a specified list.
Example
# Simple example of List copy() Method
# given list
car_list = ["Tata", "Honda", "Mahindra", "Toyota", "BMW"]
print("Given List:", car_list)
# creating a copy of a list using copy()
newcar_list = car_list.copy()
print("Copied List:", newcar_list)
Output:
Given List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW']
Copied List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW']
Explanation:
In the preceding example, a list is presented. We utilized the copy method to produce a shallow copy of the original list and assigned the resulting list to a new variable named newcar_list.
Consequently, we have effectively duplicated the elements of the specified list into a newly created list.
Example 2: Modifying the Copied List
Let’s examine an example to understand the effects of attempting to alter the duplicated list.
Example
# Simple example of List copy() Method
# given list
car_list = ["Tata", "Honda", "Mahindra", "Toyota", "BMW"]
# creating a copy of a list using copy()
newcar_list = car_list.copy()
# modifying the copied list
newcar_list.append("Audi")
# printing results
print("Given List:", car_list)
print("Modified List:", newcar_list)
Output:
Given List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW']
Modified List: ['Tata', 'Honda', 'Mahindra', 'Toyota', 'BMW', 'Audi']
Explanation:
In this illustration, we have a list provided for our use. We employed the copy method to duplicate the list, saving it in a variable named newcar_list. Subsequently, we utilized the append method to insert a new element into the duplicated list.
Consequently, following the addition of 'Audi' to newcar_list, the initial list remains unaffected. This demonstrates that the two lists operate independently of one another.
Example 3: Copying a List of Lists
Although the copy method enables the creation of a new list, it is important to note that this is a shallow copy. Alterations made to the inner lists of the copied list will also influence the original list, as both lists share references to the same inner lists.
Example
# Simple example of List copy() Method
# given list
matrix = [
[1, 3],
[5, 2]
]
# creating a copy of a list using copy()
new_matrix = matrix.copy()
# modifying the copied list
new_matrix[0][0] = 99
print("Given List:", matrix)
print("Modified List:", new_matrix)
Output:
Given List: [[99, 3], [5, 2]]
Modified List: [[99, 3], [5, 2]]
Explanation:
In the preceding example, we have been presented with a collection of lists. We utilized the copy method to generate a shallow duplicate of the original list. Subsequently, we altered an item within the nested list of the duplicated list.
Even though the two lists exist independently, the nested lists contained within them continue to share the same reference. Consequently, altering an item within the nested structure of the copied list will also influence the original list. This phenomenon is what characterizes it as a shallow copy.
Example 4: Copying a List of Mixed Data Types
In this example, we will generate a shallow copy of a list that contains various data types, including integers, strings, lists, and dictionaries.
Example
# Simple example of List copy() Method
# given list
mixed_list = [10, 'Hello', 2.4, [3, 6], {'logicpractice' : 'tech'}]
# creating a copy of a list using copy()
new_mixed_list = mixed_list.copy()
# modifying the copied list
new_mixed_list[3].append(11)
# printing results
print("Given List:", mixed_list)
print("Modified List:", new_mixed_list)
Output:
Given List: [10, 'Hello', 2.4, [3, 6, 11], {'logicpractice': 'tech'}]
Modified List: [10, 'Hello', 2.4, [3, 6, 11], {'logicpractice': 'tech'}]
Explanation:
In the preceding illustration, a collection containing various data types is presented. We employed the copy function to generate a shallow replica of the original list. Following that, we altered the nested list within the duplicated list by adding an additional element to it.
Even though the two lists are distinct, the inner lists contained within them continue to reference the same memory location. Therefore, when the value 11 is added to newmixedlist[3], it simultaneously alters mixed_list[3] as well.
Conclusion
Python offers various built-in methods and functions that assist programmers in efficiently developing error-free applications. The list copy method in Python is particularly beneficial when the goal is to generate a shallow copy. This tutorial has provided comprehensive information and examples related to this method; however, do not confine yourself to these illustrations; instead, experiment with additional examples to enhance your proficiency.
Python List copy Method FAQs
- What functionality does the copy method provide in Python?
The copy method in Python lists is a built-in function that utilizes a reference from an existing list to replicate its elements, thereby creating a new shadow list.
- Is it possible to apply copy across all data types in Python?
The copy function is applicable to mutable data types, including dictionaries, lists, and sets.
- When it comes to lists, which is more efficient: using copy or slicing?
The Slicing technique offers superior speed compared to the copy function; however, the copy method is favored for its clarity and professionalism in coding.