To fix the AttributeError: ‘list’ object has no attribute ‘encode’ error in Python, you need to call the encode() function on the string or access the element in the list and then do the encoding of it. Post details below.
What causes the AttributeError: ‘list’ object has no attribute ‘encode’?
AttributeError is one of the exceptions in Python. AttributeError occurs when you access an undefined property on an object.
The error happens when you call to encode() on a list instead of encode(), which must be called on a string.
Example:
listOfStr = ['John', '18yearsold', 'Dev'] # Use the encode() function to encode. newList = listOfStr.encode('utf-8') print(newList)
Output:
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
AttributeError: 'list' object has no attribute 'encode'
How to solve this error?
Use the encode() function on the string
Example:
- Convert the list to a string and call the encode() function on the string.
listOfStr = ['John', '18yearsold', 'Dev'] # Convert a list to a string using the str() function. newStr = str(listOfStr) # Use the encode() function to encode a string. result = newStr.encode('UTF-8') print('Object after encrypted:', result)
Output:
Object after encrypted: b"['John', '18yearsold', 'Dev']"
You can use the isinstance() function or the type() function to check the object data type.
Example:
listOfStr = ['John', '18yearsold', 'Dev'] if isinstance(listOfStr, list) is True: # Convert a list to a string using the str() function. newStr = str(listOfStr) # Use the encode() function to encode a string. result = newStr.encode('UTF-8') print('Object after encrypted:', result)
Output:
Object after encrypted: b"['John', '18yearsold', 'Dev']"
Example:
listOfStr = ['John', '18yearsold', 'Dev'] if type(listOfStr) is list: # Convert a list to a string using the str() function. newStr = str(listOfStr) # Use the encode() function to encode a string. result = newStr.encode('UTF-8') print('Object after encrypted:', result)
Output:
Object after encrypted: b"['John', '18yearsold', 'Dev']"
Encode each element in the list
Example:
- Access each element in the list.
- Encode each of those elements using the encode() function.
listOfStr = ['John', '18yearsold', 'Dev'] # Access each list element and then encrypt each of them. idx1 = listOfStr[0].encode('UTF-8') idx2 = listOfStr[1].encode('UTF-8') idx3 = listOfStr[2].encode('UTF-8') print(idx1) print(idx2) print(idx3)
Output:
b'John'
b'18yearsold'
b'Dev'
Encode each element in the list using list comprehension.
Example:
listOfStr = ['John', '18yearsold', 'Dev'] # Use list comprehension to encode each element in the list. encodeList = [ i.encode('UTF-8') for i in listOfStr ] print('Objects after being encrypted:', encodeList)
Output:
Objects after being encrypted: [b'John', b'18yearsold', b'Dev']
Summary
So the error AttributeError: ‘list’ object has no attribute ‘encode’ in Python was neatly resolved. For this error, the critical point is that the encode() function must be called on the string. If there is a better way or the article contains incorrect information, please comment below.
Maybe you are interested:
- AttributeError: ‘dict’ object has no attribute ‘add’
- AttributeError module ‘pandas’ has no attribute ‘read_csv’
- AttributeError: ‘float’ object has no attribute ‘X’ in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java