TypeError: can only join an iterable in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# TypeError: can only join an iterable in Python

The Python "TypeError: can only join an iterable" occurs when you 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 is caused because the list.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).

# Make sure to pass an iterable to the str.join() method

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'

make sure to pass iterable to str join

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.

# Most common sources of None in Python

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.

# Functions that don't return anything return None

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']) # โ›”๏ธ TypeError: can only join an iterable my_str = ' '.join(get_list())

functions that dont return anything return none

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"

The function now returns a list, so we can safely pass the list to the join() method.

# Checking if a variable doesn't store None before calling join()

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')

check if variable does not store none before calling join

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() or reverse) that mutate the original object in place and return None.

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

# A function only returning a value if a condition is met

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'])

function only returning value if condition is met

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 an empty list if the condition is not met # ๐Ÿ‘‡๏ธ [] my_list = get_list(['a', 'b'])

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

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.

Copyright ยฉ 2024 Borislav Hadzhiev