• Register
2 votes
31 views

Problem:

I am quite a noob in python programming. I just started learning how to code with python. Now I am working with python list and trying to handle it. Could anybody help me here by telling me, how to copy a list in python, please?

I would appreciate it the most.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes
 
Best answer

Solution:

There are multiple ways to do the copy/clone a python list

  • Using slicing technique
  • Using the extend() method
  • Using the list() method
  • Using the method of Shallow Copy
  • Using list comprehension
  • Using the append() method
  • Using the copy() method
  • Using the method of Deep Copy

Among all of them, the first three are popular, and the fastest way to cloning list.

Slicing Technique:

# Python program to copy or clone a list 
# Using the Slice Operator 
def Cloning(li1): 
    li_copy = li1[:] 
    return li_copy 
  
# Driver Code 
li1 = [4, 8, 2, 10, 15, 18] 
li2 = Cloning(li1) 
print("Original List:", li1) 
print("After Cloning:", li2) 

This is the fastest and easiest way to clone a list. This method used when we need to modify a list and keep a copy of an original list.

 

extend() Method:

# Python code to clone or copy a list 
# Using the in-built function extend() 
def Cloning(li1): 
    li_copy = [] 
    li_copy.extend(li1) 
    return li_copy 
  
# Driver Code 
li1 = [4, 8, 2, 10, 15, 18] 
li2 = Cloning(li1) 
print("Original List:", li1) 
print("After Cloning:", li2)

list() Method:

# Python code to clone or copy a list 
# Using the in-built function list() 
def Cloning(li1): 
	li_copy = list(li1) 
	return li_copy 

# Driver Code 
li1 = [4, 8, 2, 10, 15, 18] 
li2 = Cloning(li1) 
print("Original List:", li1) 
print("After Cloning:", li2)

This is the simplest way to make a clone of a list by using the built-in list() function.

13 9 6
94,240 points

Related questions

0 votes
1 answer 6 views
6 views
Problem: Is there any way to make copy of a list? Please suggest possibles examples.
asked Nov 2, 2020 Mubashi-Question 1.7k points
0 votes
1 answer 38 views
38 views
I’m not an expert in C++ and hoping for some help here in this issue.
asked Dec 7, 2020 TeamScript 13.5k points
0 votes
1 answer 4 views
4 views
Problem: copy() method, changes made to one list or not reflected on other except for in nested elements (like list within list), Here i should use copy. How Python includes a built-in function to support creating a shallow copy of a list: copy(). You can use the copy() method to replicate a list and leave?
asked 5 days ago Mashhoodch 9.3k points
0 votes
1 answer 12 views
0 votes
2 answers 12 views
12 views
Problem: How a list is the most flexible data structure in Python. Whereas, a 2D list which is commonly known as a list of lists, is a list object where every item is a list itself - for example: [[1,2,3], [4,5,6], [7,8,9]].
asked Jan 26 Mashhoodch 9.3k points
0 votes
1 answer 15 views
15 views
Problem: I have a rundown in Python. I need to channel a section without utilizing Pandas. I have attempted as indicated below. But it doesn't work. Data: For example, I will do some calculations only for Country = 'Canada'. filtered = [x for x in data if data ... which the percentage increase was the highest. View columns: Country, 1960, 1961, 1962, ... to 2017. Data in Python: (after splitting)
asked Dec 30, 2020 sasha 13.2k points
0 votes
1 answer 10 views
10 views asked Oct 24, 2020 Shaad Sh 800 points
0 votes
1 answer 42 views
42 views asked Oct 23, 2020 Shaad Sh 800 points
0 votes
1 answer 26 views
26 views asked Oct 23, 2020 Shaad Sh 800 points
0 votes
1 answer 12 views
12 views asked Oct 8, 2020 jagruthi 680 points