The python copy() method returns a new list. Users would sometimes like to reuse an object. For such purposes, the python copy list method will be of great use. This function is used to generate a copy of the list as needed by the user.
Python list copy
For example:
prime_num1 = [2, 3, 5, 7, 11]
# copying the list using copy()
numbs = prime_numb1.copy()
print('The duplicated list is:', numbs)
Output:
The duplicated list is: [2, 3, 5, 7, 11]
Syntax:
x=list.copy()
python copy list parameters
The copy() does not take any parameters as input from the users.
Return Value from copy()
A shallow list will be returned as output after using the copy(). Any changes made to the duplicated shallow list will no be reflected in the original list.
Example 1: Copying a List
Method 1: Using the copy()
In the example given below, the user wants to make a copy of the ‘my_lst1’ using the copy().Â
Source Code:
my_lst1 = ['doja_cat', 1, 3, 69, 0, 6.7]
# copying the list
n_list111 = my_lst1.copy()
print('The copied list is:', n_list111)
Output
The copied list is: ['doja_cat', 1, 3, 69, 0, 6.7]
Method 2: using = operator
A list can be copied in python using the ‘= operator.’ In this method, if you make any changed to the new list that is copied the changes will be reflected in the original list too. This is because by using the = operator, the compiler is copying the referencing too.Â
Source Code:
o_list1 = [7, 9, 1, 2, 3]
# copy list using =
n_list2 = o_list1
# add an element to list
n_list2.append('a')
print('New List:', n_list2)
print('Old List:', o_list1)
Output
Old List: [7, 9, 1, 2, 3, 'a']
New List: [7, 9, 1, 2, 3, 'a']
Example 2: Copy List Using Slicing Syntax
In the program given below, the user has copied the list using the slicing technique.Â
Source Code:
origi_list = ['catto', 0, 89, 2, 6.7]
# copying a list using slicing
new_list = origi_list[:]
# Adding an element to the new list
new_list.append('doggo')
# Printing new and old list
print('Old List is:', origi_list)
print('New Lis is:', new_list)
Output
Old List is: ['catto', 0, 89, 2, 6.7]
New Lis is: ['catto', 0, 89, 2, 6.7 'doggo']
How to copy a list in python
listt1 = [0, 1, 2, 3, 4, 5, 6, 7 ]
Â
listt2 = listt1.copy()
Â
print ("The new list is : " + str(listt2))
Â
# Adding a new element to new list
listt2.append(33)
Â
# Printing the list after adding a new element
print ("The new list : " + str(lis2))
#As we are creating a shallow copy there will be no change in the old list
print ("The old list : " + str(lis1))
Output:Â
The new list created is : [0, 1, 2, 3, 4, 5, 6, 7 ]
The new list: [0, 1, 2, 3, 4, 5, 6, 7, 33]
The old list : [0, 1, 2, 3, 4, 5, 6, 7 ]
How do I copy one list to another?
In the python programming language, the users can copy one list to the other list by using the in-built copy(). This creates a shallow list, meaning any changes made to the modified list will not be reflected in the original list.Â
How do you copy a list without references in python?
In python, users can copy a list without references by using the copy(). The copy() generates a shallow list that does not include the references. Any changes made to the new list will not be reflected in the original list.Â
What does Copy () do in python?
The copy() in python is used to generate a shallow copied list. For example:
Source Code
my_fruits = ["strawberry", apple", "banana", "orange", "cherry"]
ax = my_fruits.copy()
print(ax)
Output:
["strawberry", apple", "banana", "orange", "cherry"]
Leave a Reply