AttributeError: ‘list’ object has no attribute ‘copy’ error typically occurs in Python because the “list copy() method does not exist in python 2.x.” The copy method was added in Python 3.3. If you’re using a version prior to this (which is unlikely but possible), the copy() method will not be available.
How to fix it?
To fix the AttributeError: ‘list’ object has no attribute ‘copy’ error in Python, “upgrade the Python to the latest version or use the alternatives of copy() method.”
Alternatives to using the copy() method
To copy a list and can’t use the copy method for some reason, you can use one of the following alternatives:
- Using slicing: new_list = old_list[:]
- Using the list constructor: new_list = list(old_list)
- Using the copy module: import copy; new_list = copy.copy(old_list)
Using slicing
Slicing is one of the most straightforward ways to copy a list in Python.
new_list = old_list[:]
Example
# Original list
old_list = [1, 2, 3, 4, 5]
# Copying list using slicing
new_list = old_list[:]
# Modifying new list
new_list.append(6)
# Showing that the old list is unchanged
print("Old List:", old_list)
print("New List:", new_list)
Output
Old List: [1, 2, 3, 4, 5]
New List: [1, 2, 3, 4, 5, 6]
Using the list constructor
The list constructor is another way to create a shallow copy of a list.
new_list = list(old_list)
Example
# Original list
old_list = [1, 2, 3, 4, 5]
# Copying list using list constructor
new_list = list(old_list)
# Modifying new list
new_list.append(6)
# Showing that the old list is unchanged
print("Old List:", old_list)
print("New List:", new_list)
Output
Old List: [1, 2, 3, 4, 5]
New List: [1, 2, 3, 4, 5, 6]
Using copy module
The copy module in Python provides a copy() function that can create shallow copies of objects. For lists, using copy.copy() is effectively the same as using slicing or the list constructor in terms of creating a shallow copy.
import copy
new_list = copy.copy(old_list)
Example
import copy
# Original list
old_list = [1, 2, 3, 4, 5]
# Copying list using copy.copy()
new_list = copy.copy(old_list)
# Modifying new list
new_list.append(6)
# Showing that the old list is unchanged
print("Old List:", old_list)
print("New List:", new_list)
Output
Old List: [1, 2, 3, 4, 5]
New List: [1, 2, 3, 4, 5, 6]
That’s it!
Related posts
AttributeError: ‘list’ object has no attribute ‘str’
AttributeError: ‘list’ object has no attribute ‘split’
AttributeError: ‘list’ object has no attribute ‘ndim’

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.