How to Solve Python TypeError: can only join an iterable

by | Programming, Python, Tips

The join() takes all items in an iterable and joins them into one string. If you attempt to pass a non-iterable object to the join() method, you will raise the error: Python TypeError: can only join an iterable.

You can solve this by ensuring that you do not assign the result of any method that performs in-place to a variable with the same name as the iterable you want to join. If you do this, you will pass a None object to the join() method, which is non-iterable.

This tutorial will go through the error in detail and an example to learn how to solve it.


TypeError: can only join an iterable

TypeError tells us that we are trying to perform an illegal operation for a specific Python data type.

There are data types like NoneType that are non-iterable. As the error message suggests, join() is an illegal operation for non-iterable objects.

Example: Sorting a List of Strings

Let’s look at an example where we define a list of names we want to join. However, we want to sort the names in ascending order using the sort() method before joining. The code would look as follows:

names = ["Bill", "Zeus", "Griffith", "John", "Daniel", "Andrew"]

names = sort(names)

names = names.sort()

sorted_names = ' '.join(names)

print(sorted_names)

Let’s run the code to see the result:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
sorted_names = ' '.join(names)

TypeError: can only join an iterable

Our program throws the error because the sort() method performs in-place sorting and returns a None value. If we assign the output of names.sort() to names, the variable will be None when we pass it to the join() method. None is a non-iterable data type, and join() is illegal on non-iterable objects.

Solution

To solve the problem, we must keep the names variable as a list, not a None object. We do not need to assign the output of the names.sort() to a variable, and we can call the method and sort the list in place. Let’s look at the revised code:

names = ["Bill", "Zeus", "Griffith", "John", "Daniel", "Andrew"]

names.sort()

sorted_names = ' '.join(names)

print(sorted_names)

Let’s run the code to see what happens:

Andrew Bill Daniel Griffith John Zeus

Our code successfully sorts the list of names, joins them with an empty-space separator, and prints the result to the console.

Summary

Congratulations on reading to the end of this tutorial! If you attempt to pass a non-iterable object like a None to the join() method, you will raise the error: Python TypeError: can only join an iterable. This error commonly occurs when you use a function that performs and assigns the result to the variable you want to join. If a function performs in place and returns a None value, you do not need to assign it to a variable, for example, sort(). To solve this error, ensure you are using the function without assignment.

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨