Python list copy Function | copying(cloning) a List examples

  • by

The Python list copy method returns a copy of the specified list. It returns a shallow copy of the list.

What is a shallow copy?

A shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. So, if you need the original list unchanged when the new list is modified, you can use the copy() method.

Techniques to shallow copy :

  • Using copy.copy()
  • Using list.copy()
  • Using slicing

Syntax

list.copy()

Parameter Values

It doesn’t return any value and doesn’t modify the original list.

Python list copy Function Examples

Copy/clone List example

In the example Copying the list of the fruits. It’s a copy list without changing the original list.

fruits = ['apple', 'banana', 'cherry', 'orange']

copy_fruits = fruits.copy()

print(copy_fruits)

Output:

Python list copy Function

Python list copy deep

Techniques to deep copy :

  • Using copy.deepcopy()
  • Using “=” operator

Example of deep copy

import copy

list1 = [1, 2, 3, 4]

# Using deep copy techniques to create a deep copy
list2 = list1
lis3 = copy.deepcopy(list1)

# Adding new element to new lists
list2.append(5)
lis3.append(5)

# Printing lists after adding new element
# changes reflected in old list
print("The new list created using copy.deepcopy() : " + str(list2))
print("The new list created using = : " + str(lis3))
print("The old list  : " + str(list2))

Output:

The new list created using copy.deepcopy() : [1, 2, 3, 4, 5]
The new list created using = : [1, 2, 3, 4, 5]
The old list : [1, 2, 3, 4, 5]

Copy list by value in Python

A way to copy a list by value:

new_list = old_list[:]

If the list contains objects and you want to copy them as well, use a generic copy.deepcopy():

import copy
new_list = copy.deepcopy(old_list)

Deep Copy vs Shallow copy

The difference between Deep Copy vs Shallow copy is that Deep copy means if we modify any of the lists, changes are reflected in both lists as they point to the same reference. Whereas in shallow copy, when we add an element in any of the lists, only that list is modified.

Q: How to python copy a list of lists?

Answer: Same as above you can use the copy function to copy the list of the list:

list1 = [[1, 2], [3, 4]]

list2 = list1.copy()

print(list2)

Output: [[1, 2], [3, 4]]

Q: What are the options to clone or copy a list in Python?

Answer: You can Copy the list in various ways like:

You can use the built-in list.copy() method (available since Python 3.3):

new_list = old_list.copy()

You can slice it:

new_list = old_list[:]

You can use the built-in list() function:

new_list = list(old_list)

You can use generic copy.copy():

import copy
new_list = copy.copy(old_list)

This is a little slower than list() because it has to find out the datatype of old_list first.

If the list contains objects and you want to copy them as well, use generic copy.deepcopy():

import copy
new_list = copy.deepcopy(old_list)

Complete Example

import copy

class Foo(object):
    def __init__(self, val):
         self.val = val

    def __repr__(self):
        return 'Foo({!r})'.format(self.val)

foo = Foo(1)

a = ['foo', foo]
b = a.copy()
c = a[:]
d = list(a)
e = copy.copy(a)
f = copy.deepcopy(a)

# edit orignal list and instance 
a.append('baz')
foo.val = 5

print('original: %r\nlist.copy(): %r\nslice: %r\nlist(): %r\ncopy: %r\ndeepcopy: %r'
      % (a, b, c, d, e, f))

Output:

Python list copy Function

Do comment if you have any examples, doubts, and suggestions on this tutorial.

Note:
IDE: PyCharm 2020.1.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.