Last updated: Apr 8, 2024
Reading timeยท3 min

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.

Here is an example of how the error occurs.
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).
join() with the result of calling a method (e.g. reverse or sort) or a function that doesn't return anything (returns None).str.join() methodTo solve the error, we have to make sure to pass an iterable to the str.join()
method, e.g. a list or a tuple.
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.
join() with and make sure it is an iterable.If the value is None, the most common sources of None values are:
None implicitly).None.reverse, sort, etc).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.
# ๐๏ธ this function returns None def get_list(): print(['kiwi', 'banana', 'apple']) # โ๏ธ TypeError: can only join an iterable my_str = ' '.join(get_list())

You can use a return statement to return a value from a function.
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.
Use an if statement if you need to check
whether a variable doesn't store a None value
before joining it into a string.
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.
my_list = None if my_list is None: my_list = [] my_str = ','.join(my_list) print(my_str) # ๐๏ธ ''
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.
Another common cause of the error is having a function that returns a value only if a condition is met.
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.
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.
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.