Python TypeError: can only join an iterable Solution
With the help of a Python string’s join() method, we can concatenate all the string values of an iterable object to a new string. The join() method accepts the iterable object as an argument value and concatenates the strings present in the iterable object.
But if we pass a non-iterable object to the join() method, we will encounter the TypeError: can only join an iterable Error.
In this Python guide, we will discuss this error in detail and see how to debug it in a Python program. We will also walk through an example to understand this error in detail.
So let’s get started with the error statement.
Python Error: TypeError: can only join an iterable
Python string is a sequence of characters, and if we wish to concatenate an iterable object of string values as a single list object, we can use the string join() method.
The string join() method accepts an iterable object of string values as an argument and join all the string values of the iterable with the separator string value.
Syntax
Example
The join() method only accepts an iterable object as an argument value, and if we pass a not iterable object like int , float , and function to the join method, it will raise the TypeError: can only join an iterable Error.
The Error statement TypeError: can only join an iterable has two parts Exception Type and an Error message.
- TypeError (Exception Type)
- can only join an iterable (Error Message)
1. TypeError
The TypeError is one of the Python standard exceptions, and it is raised in a Python program when we specify an inappropriate operation on a Python object or pass an invalid data type argument to a function or method.
2. can only join an iterable
The » can only join an iterable » statement is an Error Statement that tags along the Python’s TypeError. This error message is raised in a Python program when passing a non-iterable data object to the join method. The join method can only join an iterable object of string values, and if we pass a non-iterable object to a join method, it will throw the TypeError with the «can only join an iterable» message.
Common Example Scenario
Let’s say we have a list of students’ Names, and we wish to print all the students’ names as a single string value after sorting the student names in ascending order.
To sort the student name lists, we use the list sort method, and to print all the students’ names as a single string value, we can join all the students’ names using the join() method.
Example Error.
Output
Break The code In the above example, we are getting the error in line 8 with » all_students = ‘-‘.join(students) » Statement.
This is because in line 8, the value of students was None . In line 5 when we sort the students list using students.sort() method and assign the value to the students, it made the students value to None.
The list sort() method perform the in-place sorting and return a None value. And when we assign the value to students.sort() to students in line 5 the value of students became None and when we pass that None value to join () method it raised the Error.
Solution
To solve the above problem we need to ensure that the students value we are passing to the join() method must be a list, not a None object. And when we talk about the above problem, there we do not need to assign the students.sort() return value to the students . The sort() method will sort the students list in place, and we can pass that to the join() method.
Output
Conclusion
The «TypeError: can only join an iterable» error is one of the most common errors you would encounter while working with converting a list string values to a string. This Error is raised in a program when we assigned a non-iterable value to the join method. So whenever you encounter this error in your program, you need to ensure that the value you are trying to pass to the join() method is an iterable object at that part of the program.
If you are still getting this error in your Python program, please share your code and query in the comment section. We will try to help you in debugging.
Python detect and prevent TypeError: sequence item 0 and join iterable
Errors in Python are explicit and provide good amount of information. For example errors:
TypeError: sequence item 0: expected str instance, int found
TypeError: can only join an iterable
It’s TypeError and it’s describing that string is expected but is found integer. Still many beginners in programming and python suffer when the see errors like this. Below you can example code which cause the error:
the result of this execution is:
TypeError: sequence item 0: expected str instance, int found
Which means that method join expect list of string and you give list of integers. One way to solve is to convert elements of your list to strings:
then you can apply functions like join:
A better way is to iterate all the list elements and convert them to strings like:
result of which is:
Another common error for beginners is:
TypeError: can only join an iterable
This error is also obvious. Below you can find example and explanation of it:
which results into:
TypeError: can only join an iterable
As you guess the problem is that method join expect a list or iterable but receive a type which is not. Most probably this error is related to semantic error in your code and it’s better to find why do you have situation like this. Of course a quick workaround would be:
But it is much better to catch or avoid errors like this one. There are several ways of doing this:
Use try except block to ensure that all errors are catch and your code is safe:
or check the object type and then decide how to treat it:
Both ways ensure that your code is secure against errors like this last two.
Python TypeError: can only join an iterable Solution
![]()
The join() method lets you transform an iterable object such as a list into a string. It is the opposite of the split() method. If you try to use this method to join a value that is not an iterable object into a list, you’ll encounter the “TypeError: can only join an iterable” error.
In this guide, we’re going to talk about what this error means and how it works. We’ll walk through an example of this list to help you learn how it works.
TypeError: can only join an iterable
Let’s take a look at our error message: TypeError: can only join an iterable.
TypeErrors are raised when you try to perform an operation on a value whose data type does not support that operation. The message after the error type tells us that we’re trying to join a value that is not an iterable object.
The join() method only lets you join iterables like a list or a tuple. This is because the join method traverses through every item in a sequence and adds them to a string. If a value is not iterable, this process cannot occur.
This error is common when you try to join a value that is equal to None.
An Example Scenario
Let’s build a program for a cheesemonger that orders a list of cheeses. We’ll ask a user whether they want the cheeses ordered in ascending or descending order. To start, let’s define a list of cheeses:
cheeses = [«Parmesan», «English Cheddar», «Feta», «Roquefort», «Brie»]
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
Find Your Bootcamp Match
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Start your career switch today
Next, we’re going to ask the user for the order in which they want the cheese list to appear:
order = input(«Do you want the cheeses to appear in ascending (ASC) or descending (DESC) order? «)
We’re going to use an “if” statement to translate the answer a user inserts into a boolean:
If a user inserts the value “ASC”, the list will be sorted in ascending order. Otherwise, the list will be sorted in reverse order.
Next, we’re going to use the sort() method to sort our list:
We refer to the “reverse” variable we defined earlier. This lets us tell the sort() method whether we want our cheeses to be sorted in ascending or descending order.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
Now that we’ve sorted our cheeses, we are going to join them into a string and print them to the console:
The join() method joins all the cheeses from the “cheeses” list into a string. Each cheese will be separated by a space and a comma. We enclosed these characters in quotation marks in our code before we called the join() method.
Let’s execute our program:
Do you want the cheeses to appear in ascending (ASC) or descending (DESC) order? ASC
Our code returns an error.
The Solution
Our code asks the user for the order in which the list of cheeses should appear. Our code stops working on line 12. This is where we join our cheeses list into a string.
The problem is that the value of “cheeses” becomes equal to None in our code. This is because we’ve assigned the value of the sort() method to a new variable.
The sort() method sorts a list in-place. It alters an existing list. The sort() method does not return a new list. This means that when we assign the value of cheeses.sort(reverse=reverse) to a variable, the value of that variable becomes None.
To solve this problem, we’ve got to remove the variable declaration on the line of code where we sort our cheeses:
This code will sort our list of cheeses. The value of “cheeses” will be equal to the sorted list rather than None. Let’s run our code and see what happens:

«Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!»
Venus, Software Engineer at Rockbot
Find Your Bootcamp Match
Do you want the cheeses to appear in ascending (ASC) or descending (DESC) order? ASC
Your sorted cheeses are: Brie, English Cheddar, Feta, Parmesan, Roquefort
Our code successfully returns a sorted list of cheeses. Let’s try to sort our cheeses in descending order:
Do you want the cheeses to appear in ascending (ASC) or descending (DESC) order? DESC
Your sorted cheeses are: Roquefort, Parmesan, Feta, English Cheddar, Brie
Our code is capable of sorting our lists both in ascending and descending order.
Conclusion
The “TypeError: can only join an iterable” error is caused when you try to join a value that is not iterable to a string.
This can happen if you assign the value of a built-in list method such as sort() to a new variable and try to join the result of that operation to a string. You can fix this error by ensuring that the value that you try to join into a string is an iterable, like a list or a tuple.
Now you’re ready to solve this error in your code like a professional!