Friday, December 27, 2024
Google search engine
HomeLanguagesPython List copy() Method

Python List copy() Method

Python List Copy() methods return a shallow copy of a list, which returns a new list without modifying the original lists. Sometimes, there is a need to reuse any object, hence copy methods are always of great utility. This article aims to demonstrate the copy method present in the list. Since the list is widely used, its copy is also necessary.

Syntax of List copy() Method

The list copy method in Python has the following syntax:

Syntax: list.copy()

Parameters: The copy() method doesn’t take any parameters.

Returns: Returns a shallow copy of a list. A shallow copy means any modification in the new list won’t be reflected in the original list. 

Python List copy Method Examples

Let us see a few examples of the list copy() method in Python.

Example 1: Simple List Copy

In this example, we are creating a List of Python strings and we are using copy() method to copy the list to another variable.

Python3




lis = ['Geeks','for','Geeks']
new_list = lis.copy()
print('Copied List:', new_list)


Output :

Copied List: ['Geeks', 'for', 'Geeks']

Example 2: Demonstrating the working of List copy() 

Here we will create a Python list and then create a shallow copy using the copy() function in Python. Then we will append a value to the copied list to check if copying a list using copy() method affects the original list.

Python3




# Initializing list 
lis1 = [ 1, 2, 3, 4 ]
  
# Using copy() to create a shallow copy
lis2 = lis1.copy()
  
# Printing new list 
print ("The new list created is : " + str(lis2))
  
# Adding new element to new list
lis2.append(5)
  
# Printing lists after adding new element
# No change in old list
print ("The new list after adding new element : \
" + str(lis2))
print ("The old list after adding new element to new list  : \
" + str(lis1))


Output :

The new list created is : [1, 2, 3, 4]
The new list after adding new element : [1, 2, 3, 4, 5]
The old list after adding new element to new list  : [1, 2, 3, 4]

Note: A shallow copy means if we modify any of the nested list elements, changes are reflected in both lists as they point to the same reference.

Shallow Copy and Deep Copy

A deep copy is a copy of a list, where we add an element in any of the lists, only that list is modified. Whereas when we use the list.copy() method. Changes made to the copied list are not reflected in the original list. The changes made to one list or not reflected on other lists except for in nested elements (like a list within a list). Here we should use the copy.deepcopy() from the copy module to avoid this problem.

  • Techniques to deep copy: 
    • Using copy.deepcopy()
  • Techniques to shallow copy: 
    • Using copy.copy()
    • Using list.copy()
    • Using slicing

To gain a deeper understanding, Refer to this article on Deep Copy vs Shallow Copy

Example 3: Demonstrating Techniques of Shallow and Deep copy  

Here we will create a list and then create a shallow copy using the assignment operator, list.copy() method, and copy.copy() method of the Python copy module. We also create a deep copy using deepcopy() in Python. Then we will make changes to the original list and see if the other lists are affected or not.

Python3




import copy
  
# Initializing list 
list1 = [ 1, [2, 3] , 4 ]
print("list 1 before modification:\n", list1)
  
# all changes are reflected
list2 = list1 
  
# shallow copy - changes to 
# nested list is reflected,
# same as copy.copy(), slicing
  
list3 = list1.copy() 
  
# deep copy - no change is reflected
list4 = copy.deepcopy(list1) 
  
list1.append(5)
list1[1][1] = 999
  
print("list 1 after modification:\n", list1)
print("list 2 after modification:\n", list2)
print("list 3 after modification:\n", list3)
print("list 4 after modification:\n", list4)


Output:

list 1 before modification:
 [1, [2, 3], 4]
list 1 after modification:
 [1, [2, 999], 4, 5]
list 2 after modification:
 [1, [2, 999], 4, 5]
list 3 after modification:
 [1, [2, 999], 4]
list 4 after modification:
 [1, [2, 3], 4]

Example 4: Copy List Using Slicing

Here we are copying the list using the list slicing method [:] and we are appending the ‘a’ to the new_list. After printing we can see the newly appened character ‘a’ is not appended to the old list.

Python3




list = [2,4,6]
new_list = list[:]
new_list.append('a')
print('Old List:', list)
print('New List:', new_list)


Output :

Old List: [2, 4, 6]
New List: [2, 4, 6, 'a']

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments