AttributeError: ‘list’ object has no attribute ‘replace’ in Python

The error “AttributeError: ‘list’ object has no attribute ‘replace'” occurs when you apply the replace() function to a list. In this article, we will show you how to change the elements of the list correctly. Let’s start.

The error “AttributeError: ‘list’ object has no attribute ‘replace'” in Python

The AttributeError: ‘list’ object has no attribute ‘replace’ error is raised when you try to call the replace() method on a list object, but this method is unavailable for lists. The replace() method is a string method, so it can only be called on string objects.

Here is an example of code that would raise this error:

Code:

myList = ["The", "cat", "is", "on", "the", "desk"]
myList.replace("on", "under")

Result:

AttributeError Traceback (most recent call last)
 in <module>
----> 2 myList.replace(1, 4)
AttributeError: 'list' object has no attribute 'replace'

Solution to the error

Remember the key that the replace() method is only available to string objects. Below are a few solutions to handle the problem.

Use the indexing notation

You can change the values of list elements directly by accessing their index. As a result, if you want to replace any elements, call the index and assign a new value. For example:

Code:

myList = ["The", "cat", "is", "on", "the", "desk"]
print("The original list:", myList)
 
# Assign a new value to the first element
myList[3] = "under"
 
print("The new list:", myList)

Result:

The original list: ['The', 'cat', 'is', 'on', 'the', 'desk']
The new list: ['The', 'cat', 'is', 'under', 'the', 'desk']

Use the replace() method to each string

If you need to change all the matched substrings in the list by a new string, loop over the list and apply the replace() method to each element as long as they are string objects. To ensure the elements are string objects, use the isinstance() function.

Code:

myList = ["The", "cat", "is", "on", "the", "desk"]
print("The original list:", myList)
 
for i in range(len(myList)):
    if isinstance(myList[i], str):
        myList[i] = myList[i].replace('on', 'under')
        
print("The new list:", myList)

Result:

The original list: ['The', 'cat', 'is', 'on', 'the', 'desk']
The new list: ['The', 'cat', 'is', 'under', 'the', 'desk']

Join the list into string then replace string

Another way to replace a list’s substrings is by joining the list’s elements into a string. Then, apply the replace() method to the string and split them.

Code:

myList = ["The", "cat", "is", "on", "the", "desk"]
print("The original list:", myList)
 
myList = " ".join(myList)
myList = myList.replace("on", "under")
myList = myList.split()
 
print("The new list:", myList)

Result:

The original list: ['The', 'cat', 'is', 'on', 'the', 'desk']
The new list: ['The', 'cat', 'is', 'under', 'the', 'desk']

Summary

In summary, the error “AttributeError: ‘list’ object has no attribute ‘replace'” occurs because you apply the replace() function to a list while it is only available to string objects. To get rid of the error, you can change the elements of the list directly by accessing their index or applying the replace() function to each string-type element of the list.

Leave a Reply

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