To join the elements in the list, you must use the join() function. While using it, you may get the AttributeError: ‘list’ object has no attribute ‘join’. Let’s find the cause and solution to this error with us!
What causes the error “AttributeError: ‘list’ object has no attribute ‘join’“?
The error happens because you call the join() function on the list.
Example:
listInfor = ['John', '18 years old', 'Dev'] # The join() function is called in the wrong way print(listInfor.join(' '))
Output:
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
print(listInfor.join(' '))
AttributeError: 'list' object has no attribute 'join'
How to solve this error?
The list must be the argument of the join() function
Example:
- Create a list.
- The list must be passed as the join() function argument.
listInfor = ['John', '18 years old', 'Dev'] # Use the join function to join the elements in the list print('List elements when concatenated:', ' '.join(listInfor))
Output:
List elements when concatenated: John 18 years old Dev
You can change the separator characters while concatenating list elements.
Example:
listInfor = ['John', '18 years old', 'Dev'] # Use the join function to join the elements in the list print('List elements when concatenated:', ' ! '.join(listInfor))
Output:
List elements when concatenated: John ! 18 years old ! Dev
Concatenate elements in a tuple
In addition to the list join() function, which can also operate on tuples, it is also used to join elements in a tuple. If you don’t want to do the concatenation of the elements on the list, you can do it on the tuple.
Example:
- Convert the list to a tuple.
- Call the join() function on the tuple that joins the tuple elements.
listInfor = ['John', '18 years old', 'Dev'] # Convert the list to a tuple myTuple = tuple(listInfor) print('Elements in a tuple:', myTuple) # Use the join function to join the elements in the list print('Tuple elements when concatenated:', ' ! '.join(myTuple))
Output:
Elements in a tuple: ('John', '18 years old', 'Dev')
Tuple elements when concatenated: John ! 18 years old ! Dev
Note: Integer appears in both list and tuple when using the join() function. It would be best if you converted it to string type. Otherwise, the program will throw the error TypeError: sequence item 3: expected str instance, int found
.
Example:
- Integer 18 converts to string type, so there is no error.
listInfor = ['John', '18 years old', 'Dev', '18'] # Convert the list to a tuple myTuple = tuple(listInfor) print('Elements in a tuple:', myTuple) # Use the join function to join the elements in the list print('Tuple elements when concatenated:', ' ! '.join(myTuple))
Output:
Elements in a tuple: ('John', '18 years old', 'Dev', '18')
Tuple elements when concatenated: John ! 18 years old ! Dev! 18
Summary
I hope through this article you have an idea to fix the AttributeError: ‘list’ object has no attribute ‘join’ in Python. I suggest you to use the method: The list must be the argument of the join() function to fix this error. Leave a comment so I can know how you feel about the article. Thanks for reading!
Maybe you are interested:
- AttributeError: ‘dict’ object has no attribute ‘add’
- AttributeError: ‘str’ object has no attribute ‘loads’
- AttributeError: ‘int’ object has no attribute ‘isdigit’

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