Python has a .join()
method that helps you combine all elements in a list
into a string
. However, when using this method, many developers get the error message, which is TypeError: Can only join an iterable
. In this article, we will find out the cause of this error and the solution to it!
Cause Of “TypeError: can only join an iterable” in Python
Here is a sample: you have a list of unsorted fruit names like so:
lstFruit = ["Apple", "Orange", "Banana", "Durian"]
You want to sort the list in alphabetical order and then combine all these fruits into a string using the method .join()
.
The sample is below:
# List of unsorted fruit names lstFruit = ["Apple", "Orange", "Banana", "Durian"] # Sort the fruit names in alphabetical order lstFruit = lstFruit.sort() # Combine all the fruit names in the list into a string strFruit = " ".join(lstFruit) print(strFruit)
Unfortunately, an error message shows up:
strFruit = " ".join(lstFruit)
TypeError: can only join an iterable
The cause for this issue is easy to explain. The .join()
method only receives an iterable
as its parameter. The error appears when you try to add a non-iterable
to the .join()
.
The problem is not due to the .join()
method but how you sort the list. The .sort()
function in Python works in place and returns a None
value. In other words, .sort()
does not return a list
but only a non-iterable
.
How to solve TypeError: can only join an iterable in Python
Step 1: Make the list an iterable
As mentioned, the .sort()
method does not return an iterable
. Therefore, when you use the command:
[list] = [list].sort()
The list
will get the None
value.
You can see the sample code below:
# List of unsorted fruit names lstFruit = ["Apple", "Orange", "Banana", "Durian"] print("Original lstFruit: ", lstFruit) # Sort the fruit names in alphabetical order lstFruit = lstFruit.sort() print("lstFruit after sorted: ", lstFruit)
The output will be:
Original lstFruit: ['Apple', 'Orange', 'Banana', 'Durian']
lstFruit after sorted: None
The .sort()
sorts the list
in place. Therefore, you should not assign the result of the .sort()
to a variable. You should just call the method alone. Then the list
will be sorted without receiving the None
value. The sample is below:
# List of unsorted fruit names lstFruit = ["Apple", "Orange", "Banana", "Durian"] print("Original lstFruit: ", lstFruit) # Sort the fruit names in alphabetical order lstFruit.sort() print("lstFruit after sorted: ", lstFruit)
The output will be:
Original lstFruit: ['Apple', 'Orange', 'Banana', 'Durian']
lstFruit after sorted: ['Apple', 'Banana', 'Durian', 'Orange']
Besides the .sort()
method, many other methods, such as .remove()
and .reverse()
, also return a None
value. Therefore, you can only put the command alone to keep the list
as an iterable
.
Step 2: Transform the list into a string using the .join() method
Since the list
is iterable
, you can use the method .join()
to transform it into a string
without receiving any issues.
The command is:
[String variable] = " ".join([List])
The list
does not have a None
value now. Therefore, you can use it as the parameter for the .join()
method.
The sample code is below:
# List of unsorted fruit names lstFruit = ["Apple", "Orange", "Banana", "Durian"] print("Original lstFruit: ", lstFruit) # Sort the fruit names in alphabetical order lstFruit.sort() print("lstFruit after sorted: ", lstFruit) # Combine all the fruit names in the list into a string strFruit = " ".join(lstFruit) print("String fruits: ", strFruit)
The output will be:
Original lstFruit: ['Apple', 'Orange', 'Banana', 'Durian']
lstFruit after sorted: ['Apple', 'Banana', 'Durian', 'Orange']
String fruits: Apple Banana Durian Orange
Summary
The TypeError: can only join an iterable
in Python occurs every time you try to put a non-iterable
as a parameter for the .join()
method. You need to keep in mind that several methods that work with a list
, including .remove()
, .sort()
, and .reverse()
, will only return a None
value, which is not iterable
. Thus, the solution is to make a list
as an iterable
before using the .join()
!
I am William Nguyen and currently work as a software developer. I am highly interested in programming, especially in Python, C++, Html, Css, and Javascript. I’ve worked on numerous software development projects throughout the years. I am eager to share my knowledge with others that enjoy programming!