In your example, both 'b' and 'a' are referring to an object. You should make a separate copy if you wish to change each of them independently. To solve it you have different ways:
Option 1:
def Cloning(li1):
li_copy = li1[:]
return li_copy
a = [1, 2, 3,]
b = Cloning(a)
b[0] = 100
print("Original List:", a)
print("After Cloning:", b)
2:
# Using the in-built function extend()
def Cloning(li1):
li_copy = []
li_copy.extend(li1)
return li_copy
# Driver Code
a = [1, 2, 3]
b = Cloning(li1)
b[0] = 100
print("Original List:", a)
print("After Cloning:", b)
Option 3:
# Using the in-built function list()
def Cloning(li1):
li_copy = list(li1)
return li_copy
# Driver Code
a = [1, 2, 3]
b = Cloning(li1)
b[0] = 100
print("Original List:", a)
print("After Cloning:", b)
Option 4:
# Using list comprehension
def Cloning(li1):
li_copy = [i for i in li1]
return li_copy
# Driver Code
a = [1, 2, 3]
b = Cloning(li1)
b[0] = 100
print("Original List:", a)
print("After Cloning:", b)
Option 5:
# Using append()
def Cloning(li1):
li_copy =[]
for item in li1: li_copy.append(item)
return li_copy
# Driver Code
a = [1, 2, 3]
b = Cloning(li1)
b[0] = 100
print("Original List:", a)
print("After Cloning:", b)
Option 6:
# Using bilt-in method copy()
def Cloning(li1):
li_copy =[]
li_copy = li1.copy()
return li_copy
# Driver Code
a = [1, 2, 3]
b = Cloning(li1)
b[0] = 100
print("Original List:", a)
print("After Cloning:", b)
If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch