. Advertisement .
..3..
. Advertisement .
..4..
Are you working on the Python project? Having TypeError: can only join an iterable? Whether you are a programmer or a beginner, TypeErrors are common for both, while it is true that beginners usually get stuck in the middle of the coding when they get a TypeError message in return. TypeError: can only join an iterable is one of those many errors that need to be solved to continue designing Python Projects.
The TypeError occurs when a non-iterable value is passed to the ‘str.join()
method. Let’s discuss how to get through the TypeError: can only join an iterable
How to fix TypeError: can only join an iterable
As we have already shared above, the cause of the error is when a non-iterable value is passed to the ‘str.join()
method, and it’s like calling a ‘None’, which is a built-in method that returns nothing. So, the question arises that what are the possible ways to solve the TypeError: can only join an iterable.
First, you should check how this TypeError occurs.
Code:
sea_animals = ['Whale', 'Octopus', 'Shark', 'Oyster']
change_list = sea_animals.reverse()
print(change_list)
my_list = ' // '.join(change_list)
print(my_list)
Output:
None
Traceback (most recent call last):
File code.py", line 4, in <module>
my_list = ' // '.join(change_list)
TypeError: can only join an iterable
Here, you landed in trouble because the ‘reverse()
’ method changes the original list, and in return, you get nothing.
You can solve the TypeError: can only join an iterable;
Using str.join() Method
This method works to solve the type error, but you need to make sure to pass an iterable to the method such as a ‘tuple’ or a ‘list’. Take a look at the example.
Code:
sea_animals = ['Whale', 'Octopus', 'Shark', 'Oyster']
sea_animals.reverse()
my_list = ' // '.join(sea_animals)
print(my_list)
Output:
Oyster // Shark // Octopus // Whale
When an actual lust is passed to the ‘str.join()
’ method, TypeError: can only join an iterable is solved. This method can take up an iterable as an argument, and then a string can get in return, which is iterable.
Using Join() Method
When using this method, you need to be careful when calling it with the result of the function so that it doesn’t return anything. Please don’t call it with the print() function like the below code:
def sample_list():
print(['Whale', 'Octopus', 'Shark', 'Oyster'])
my_list1 = ' / '.join(sample_list())
print(my_list1)
['Whale', 'Octopus', 'Shark', 'Oyster']
Traceback (most recent call last):
File "code.py", line 4, in <module>
my_list1 = ' / '.join(sample_list())
TypeError: can only join an iterable
If you want to have a value from a function in return, use a ‘return statement.
Code:
def sample_list():
return ['Whale', 'Octopus', 'Shark', 'Oyster']
my_list1 = ' / '.join(sample_list())
print(my_list1)
Output:
Whale / Octopus / Shark / Oyster
Check the variable
If you are looking to check if a variable hasn’t stored a ‘none’, you need to use an ‘if’ statement for this before you join it into a string.
my_list2 = None
if my_list2 is not None:
string1 = ' '.join(my_list2)
print(string1)
else:
print('Variable stores None')
Output:
Variable stores None
The alternative to this is to provide a list that is empty as a fallback.
my_list2 = None
if my_list2 is None:
my_list2 = []
string1 = ','.join(my_list2)
print(string1)
Output:
# ' '
Check the Function
You need to check the function if it didn’t return ‘None
’, or even return a default value if it doesn’t meet the condition. Check the example:
def sample_list2(x):
if len(x) > 5:
return x
return []
my_list = sample_list2(['Swan', 'Owl'])
Output:
# ' '
Conclusion
There are a few ways to get through the TypeError: can only join an iterable. I hope you can find it useful!
Do share your feedback!
Leave a comment