TypeError: can only join an iterable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: can only join an iterable in Python #

The Python "TypeError: can only join an iterable" occurs when we pass a non-iterable value to the str.join() method, e.g. None from calling a built-in method that doesn't return anything. To solve the error, make sure to pass an iterable to the join() method.

typeerror can only join an iterable

Here is an example of how the error occurs.

main.py
fruits = ['kiwi', 'banana', 'apple'] new_list = fruits.reverse() print(new_list) # 👉️ None # ⛔️ TypeError: can only join an iterable my_str = ','.join(new_list) # 👈️ passing None to join() print(my_str)

The error was caused because the reverse() method mutates the original list in place and doesn't return anything (implicitly returns None).

Make sure you aren't calling join() with the result of calling a method (e.g. reverse or sort) or a function that doesn't return anything (returns None).

To solve the error, we have to make sure to pass an iterable to the str.join() method, e.g. a list or a tuple.

main.py
fruits = ['kiwi', 'banana', 'apple'] fruits.reverse() my_str = ','.join(fruits) print(my_str) # 👉️ 'apple,banana,kiwi'

We passed the actual list to the str.join() method which solved the error.

You should print the value you are calling join() with and make sure it is an iterable.

If the value is None, the most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything (e.g. reverse, sort, etc).
  4. Having a function that only returns a value if a certain condition is met.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

If the value you are passing to the join() method is None, make sure you aren't calling it with the result of a function that doesn't return anything.

main.py
# 👇️ this function returns None def get_list(): print(['kiwi', 'banana', 'apple']) my_str = ' '.join(get_list())

You can use a return statement to return a value from a function.

main.py
def get_list(): return ['kiwi', 'banana', 'apple'] my_str = ' '.join(get_list()) print(my_str) # 👉️ "kiwi banana apple"

Use an if statement if you need to check whether a variable doesn't store a None value before joining it into a string.

main.py
my_list = None if my_list is not None: my_str = ' '.join(my_list) print(my_str) else: # 👇️ this runs print('variable stores None')

Alternatively, you can provide an empty list as a fallback.

main.py
my_list = None if my_list is None: my_list = [] my_str = ','.join(my_list) print(my_str) # 👉️ ''
Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

Make sure you aren't storing the result of calling one in a variable.

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
def get_list(a): if len(a) > 3: return a # 👇️ None my_list = get_list(['a', 'b'])

The if statement in the get_list function is only run if the passed in argument has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error, you either have to check if the function didn't return None, or return a default value if the condition is not met.

main.py
def get_list(a): if len(a) > 3: return a return [] # 👈️ return empty list if condition not met # 👇️ [] my_list = get_list(['a', 'b'])

Now the function is guaranteed to return a value regardless if the condition is met.

Conclusion #

The Python "TypeError: can only join an iterable" occurs when we pass a non-iterable value to the str.join() method, e.g. None from calling a built-in method that doesn't return anything. To solve the error, make sure to pass an iterable to the join() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.