ADVERTISEMENT
ADVERTISEMENT

How to clone or copy a list in Python? Cloning or copying a list

Here, we are going to learn about the different methods to clone or copy a list in Python.
Submitted by Shivang Yadav, on April 08, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

How to clone or copy a list?

We will take a list as input from the user and then copy all the elements of the list to another list in python.

To perform this task python programming language provides multiple ways. Let's explore them,

Method 1: Using copy() method

A direct way to clone lists in python is using the inbuilt copy() method.

Syntax:

list_name.copy()

Returns a list with the same elements as the input list.

Program to clone or copy list using copy method in Python

# Python program to clone or copy list 
# using copy method

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# cloning the list 
copyList = myList.copy()

# Printing lists 
print("Entered List ", myList)
print("Cloned List ", copyList)

Output:

Enter number of elements : 5
10
20
30
40
50
Entered List  [10, 20, 30, 40, 50]
Cloned List  [10, 20, 30, 40, 50]

Method 2: Using shallow copy or deep copy method

Deep copy and shallow copy are coping techniques used in Python.

Python provides methods to perform both types of copies in its copy library.

Syntax:

Deep copy: 
	copy.deepcopy(list_name)

Shallow copy:
	copy.copy(list_name)

Both perform the same task of coping list but differ in performance.

Program to clone a list using deep or shallow copy technique

# Python program to clone or copy list using 
# deep copy or shallow copy method

import copy

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# cloning the list 
shallowCopyList = copy.copy(myList)
deepCopyList = copy.deepcopy(myList)

# Printing lists 
print("Entered List ", myList)
print("Deep Copy List ", deepCopyList)
print("Shallow Copy List ", shallowCopyList)

Output:

Enter number of elements : 5
10
20
30
40
50
Entered List  [10, 20, 30, 40, 50]
Deep Copy List  [10, 20, 30, 40, 50]
Shallow Copy List  [10, 20, 30, 40, 50]

Method 3: Using list slicing

List slicing method is used to slice lists and return sliced parts. But we can clone the list using this method also.

Syntax:

list[:]

Python program to clone or copy list using slicing

# Python program to clone or copy list using slicing

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# cloning the list 
copyList = myList[:]

# Printing lists 
print("Entered List ", myList)
print("Cloned List ", copyList)

Output:

Enter number of elements : 5
10
20
30
40
50
Entered List  [10, 20, 30, 40, 50]
Cloned List  [10, 20, 30, 40, 50]

Method 4: Using extend() method

The extend() method in-built in the Python library can also be used to clone a list.

extend() method is used to append the elements of an iterable to the end of another one.

Syntax:

copyList.extent(originalList)

Program to clone a list in python using extend method

# Python program to clone or copy list 
# using extend() method

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# cloning the list 
copyList = []
copyList.extend(myList)

# Printing lists 
print("Entered List ", myList)
print("Copy List ", copyList)

Output:

Enter number of elements : 5
10
20
30
40
50
Entered List  [10, 20, 30, 40, 50]
Copy List  [10, 20, 30, 40, 50]

Method 5: Using list() method

The list() method used to create a new list can also perform the last of copying/cloning list in python. This is done by passing the original list to the list() method called while creating a list.

Syntax:

list(list)

Program to clone or copy a list in python using the list() method

# Python program to clone or copy list 
# the using extend() method

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# cloning the list 
copyList = list(myList)

# Printing lists 
print("Entered List ", myList)
print("Copy List ", copyList)

Output:

Enter number of elements : 5
10
20
30
40
50
Entered List  [10, 20, 30, 40, 50]
Copy List  [10, 20, 30, 40, 50]

Python List Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.