Computer >> Computer tutorials >  >> Programming >> Python

Python program to clone or copy a list.


In this program given a user input list. Our task is to copy or cloning the list. Here we use slicing technique. In this technique, we make a copy of the list itself, along with the reference. This process is also called cloning.

Algorithm

Step 1: Input elements of the array.
Step 2: then do cloning using slicing operator(:).

Example Code

# Python program to copy or clone a list 
# Using the Slice Operator 
def copyandcloning(cl): 
   copylist = cl[:] 
   return copylist 
# Driver Code 
A=list()
n1=int(input("Enter the size of the List ::"))
print("Enter the Element of List ::")
for i in range(int(n1)):
   k=int(input(""))
   A.append(k)
clon = copyandcloning(A) 
print("Original or Before Cloning The List Is:", A) 
print("After Cloning:", clon) 

Output

Enter the size of the  List ::6
Enter the Element of  List ::
33
22
11
67
56
90
Original or Before Cloning The List Is: [33, 22, 11, 67, 56, 90]
After Cloning: [33, 22, 11, 67, 56, 90]