Click for All Topics
.copy()
method doesn’t take any argument.
Syntax –
list.copy()
Parameters –
Example –
my_list=['hello', 'students', 'warm', 'welcome']
print("Original list:",my_list)
copied_list=my_list.copy()
print("Copied list:",copied_list)
# Output -
# Original list: ['hello', 'students', 'warm', 'welcome']
# Copied list: ['hello', 'students', 'warm', 'welcome']
.copy()
method and store the copied list in the copied_list variable.
Example 2- Let’s see if modifying the original list affects the copied list or not.
chambal=['bhind','morena','sheopur']
print("Orignal list:",chambal)
chambal_copy=chambal.copy()
print("Copied List:",chambal_copy)
chambal.append('gwalior')
print("Modify original list:",chambal)
print("Copied list:",chambal_copy)
# Ouput -
# Orignal list: ['bhind', 'morena', 'sheopur']
# Copied List: ['bhind', 'morena', 'sheopur']
# Modify original list: ['bhind', 'morena', 'sheopur', 'gwalior']
# Copied list: ['bhind', 'morena', 'sheopur']
.copy()
my_list=['hello', 'student', 'this', 'is', 'edSlash']
new_list=my_list[:]
print("Original list:",my_list)
print("Copied list:",new_list)
# Output -
# Original list: ['hello', 'student', 'this', 'is', 'edSlash']
# Copied list: ['hello', 'student', 'this', 'is', 'edSlash']
Example 4-
lst=[10,20,30,40,50]
lst1=[]
for i in lst:
lst1.append(i)
print("Original list:",lst)
print("Modified list:",lst1)
# Output -
# Original list: [10, 20, 30, 40, 50]
# Modified list: [10, 20, 30, 40, 50]
Example 5-
my_list=[2,4,6,8,10,12,14,16]
my_list1=my_list
print("Original list:",my_list)
print("Copied list:"my_list1)
my_list.append(18)
print("\nModified original list:",my_list)
print("Copied list:",my_list1)
# Output -
# Original list: [2, 4, 6, 8, 10, 12, 14, 16]
# Copied list: [2, 4, 6, 8, 10, 12, 14, 16]
# Modified original list: [2, 4, 6, 8, 10, 12, 14, 16, 18]
# Copied list: [2, 4, 6, 8, 10, 12, 14, 16, 18]
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India