List Object Has No Attribute Replace
If you’re getting the error “AttributeError: ‘list’ object has no attribute ‘replace'” in Python, it means you’re trying to call the .replace() method on a list object, rather than on a string. Lists don’t have a .replace() method, but strings do.
Here’s an example of the error you might see:
>>> my_list = [1,2,3]
>>> my_list.replace(2,3)
AttributeError: ‘list’ object has no attribute ‘replace’
To fix this error, you need to call .replace() on a string, rather than on a list. For example:
>>> my_string = “1,2,3”
>>> my_string.replace(“,”,””)
‘123’
You can also convert a list into a string, and then call .replace() on the string. For example:
>>> my_list = [1,2,3]
>>> my_string = “,”.join(str(x) for x in my_list)
>>> my_string.replace(“,”,””)
‘123’