Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
5 views
in Python by (13.2k points)
edited by

Can someone please tell me methods to copy or clone a list in Python?

Why is new_list = my_list, any modifications to  new_list  changes to my_list  everytime.

3 Answers

0 votes
by (46k points)
edited by

There are several methods one can use to copy or clone a list I am discussing a few with you:-

1.   Using the extend() method

We can copy one list into another using the extend() function. This function appends each element of the iterable object e.g. another list to the end of the new list and it takes around 0.053 seconds to process.

Example:

# To clone a copy list Python can be used

# Using the in-built function extend()

defCloning(li1):

   li_copy =[]

   li_copy.extend(li1)

   returnli_copy

# Driver Code

li1 =[1,2,3,4,5,6]

li2 =Cloning(li1)

print("Original List:", li1)

print("After Cloning:", li2)

which gives the output

Original list: [1,2,3,4,5,6]

After cloning: [1,2,3,4,5,6]

2.Using list comprehension

This method is useful when one needs to copy all the elements individually from one list to another and takes around 0.217 seconds to process

Example:

# To clone a copy list Python can be used

# Using list comprehension

defCloning(li1):

   li_copy =[i fori inli1]

   returnli_copy

# Driver Code

li1 =[1,2,3,4,5,6]

li2 =Cloning(li1)

print("Original List:", li1)

print("After Cloning:", li2)

The output of the code will be

Original List: [1,2,3,4,5,6]

After Clooning:[1,2,3,4,5,6}

3.   Using slicing technique

When we want to modify a list and also keep a copy of the original we will use this method, this method is also fastest and easiest way to clone a list. This method just takes around 0.039 seconds to complete and considered as the fastest method.

Example:

# To clone a copy list Python can be used

# Using the in-built function extend()

defCloning(li1):

   li_copy =[]

   li_copy.extend(li1)

   returnli_copy

# Driver Code

li1 =[1,2,3,4,5,6]

li2 =Cloning(li1)

print("Original List:", li1)

print("After Cloning:", li2)

The output of the code will be

Original List: [1,2,3,4,5,6]

After Cloning: [1,2,3,4,5,6]

I hope this will help you with your query.

Be a Python Expert. Enroll in Python Programming Course by Intellipaat

+2 votes
by (10.9k points)
edited by

 new_list = my_list both refer to the same list because the assignment operator copies the reference to the other list but not the actual list.You may refer to the following methods to actually copy the list,

1. using list.copy() method:

new_list = my_list.copy()

2. Slice it:

new_list = my_list[:]

3. Using list() function:

new_list = list(my_list)

4.Using copy.copy()

import copy

new_list = copy.copy(my_list)

This method is a bit slow as compared to list() since it involves finding the datatype of old_list.

Using copy.deepcopy()

import copy

new_list=copy.deepcopy(my_list)

This method is used when you have objects in your list, it is also the slowest of all the method.

Ex-

import copy

class abc(object):

   def __init__(self, val):

        self.val = val

 def __repr__(self):

       return str(self.val)

abc = abc(1)

x = ['abc', abc]

y = x.copy()

z = x[:]

p = list(x)

q = copy.copy(x)

r = copy.deepcopy(x)

x.append('oop')

abc.val = 5

print('original: %r\n list.copy():  %r\n list(): %r\n deepcopy:%r\n slice:  %r\n copy:%r'

     % (x, y, z, p, q, r))

Output-

original: ['abc', 5, 'oop']

list.copy(): ['abc', 5]

list(): ['abc', 5]

deepcopy: ['abc', 1]

slice: ['abc', 5]

copy: ['abc', 5]

0 votes
by (106k points)
edited by

There are many ways to copy the list in Python see some important methods below:-

  1. The first thing you can use is the builtin function list.copy()see the code below:-
    new_list = old_list.copy()

  2. Another option you have is the slice method:-
    new_list = old_list[:]

To know more about this you can have a look at the following video tutorial:-

Learn in detail about Python by enrolling in Intellipaat Python Course online and upskill.

Related questions

0 votes
1 answer
0 votes
2 answers
asked Aug 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...