How to Solve Python AttributeError: ‘list’ object has no attribute ‘copy’

by | Programming, Python, Tips

We use lists to store multiple data values in a single variable. You cannot make a real copy of a list by typing list1 = list2, because list2 will be a reference to list1 =, and if you make changes to list1 they will be made to list2. Instead, we can use the built-in List method copy() to copy a list.

In Python major version 2, the built-in List method copy() does not exist. If you try to call the copy() method on a list using Python 2, you will raise the AttributeError: ‘list’ object has no attribute ‘copy’.

If you are using Python version 2, you can use the built-in list() method to copy a list for example, list2 = list(list1) or slicing, for example,

list2 = list1[:].

This tutorial will detail the error and solve it with code examples.


AttributeError: ‘list’ object has no attribute ‘copy’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘list’ object has no attribute ‘copy’” tells us that the list object we are handling does not have the copy() attribute. We will raise this error when calling the copy() method on a list using Python 2.

Example

Let’s look at an example where we create a dictionary and use the copy() method to get a copy of a list using Python 2.7

import sys

print(sys.version)

lst = ["jupiter", "saturn", "mercury", "venus"]

new_lst = lst.copy()

print('Copied list: ', new_lst)
2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
Traceback (most recent call last):
  File "copy.py", line 4, in <module>
    new_lst = lst.copy()
AttributeError: 'list' object has no attribute 'copy'

We throw the error because we call the copy() method on the list but copy() is not a built-in list method in Python 2. The best option to solve this error is to upgrade to Python 3. Python 2 is no longer supported. However, we will go through alternative solutions if you still want to use Python 2.

Solution #1 Using Slicing

The most common way to copy a list in Python 2 is to use slicing. Slicing involves using the indexing operator. When you omit the start and end index from the slice, the slice will be from the beginning to the end of the list. Let’s look at the revised code:

import sys

print(sys.version)

lst = ["jupiter", "saturn", "mercury", "venus"]

new_lst = lst[:]

print('Copied list: ', new_lst)

print(id(lst))

print(id(new_lst))

We also will print the id of the original and copied lists to verify they are different objects.

2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
('Copied list: ', ['jupiter', 'saturn', 'mercury', 'venus'])
140690200951264
140690201046480

We successfully copied the list and verified the lists are different objects. Because the lists are different objects, we can change the new list, and it will not change the original list.

Solution #2: Using list()

We can use the built-in list() function to copy a list. We can use list() to create a list object from any iterable. Usually, we use list() if we want a mutable object, for example, converting a tuple to a list. In this case, we will be creating a list from another list. Let’s look at the revised code:

import sys

print(sys.version)

lst = ["jupiter", "saturn", "mercury", "venus"]

new_lst = list(lst)

print('Copied list: ', new_lst)

print(id(lst))

print(id(new_lst))

We will also print the id of the original and copied lists to verify they are different objects.

2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
('Copied list: ', ['jupiter', 'saturn', 'mercury', 'venus'])
140382284511712
140382284668440

We successfully copied the list and verified the lists are different objects. Because the lists are different objects, we can change the new list, and it will not change the original list.

Solution #3: Upgrade to Python3 and Use copy()

Let’s go through an example of creating a virtual environment with Python 3 using conda.

onda create -n py38 python=3.8

We can activate the environment using the following command:

conda activate py38

You should see py38 in parentheses next to your command prompt. Now we have Python 3, we can call copy() directly on the list:

import sys

print(sys.version)

lst = ["jupiter", "saturn", "mercury", "venus"]

new_lst = lst.copy()

print('Copied list: ', new_lst)

print(id(lst))

print(id(new_lst))

Let’s run the code to see the result:

3.8.12 (default, Oct 12 2021, 06:23:56) 
[Clang 10.0.0 ]
Copied list:  ['jupiter', 'saturn', 'mercury', 'venus']
140632820110144
140632822046592

We successfully copied the list using the copy() method and verified that the original and copied lists are different objects.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘list’ object has no attribute ‘copy’ occurs when you try to call the copy() method on a list using Python major version 2. You can only use copy() in Python 3.

To solve this error, upgrade to Python 3. Otherwise, you can copy the list using slicing [:] or the built-in list() function.

For further reading on AttributeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨