Вызов ''.join ведёт к ошибке: TypeError: can only join an iterable
str.join(iterable) ожидает в качестве аргумента iterable — объект поддерживающий итерирование.
iterable An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list , str , and tuple ) and some non-sequence types like dict , file objects, and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed ( zip() , map() , . ). When an iterable object is passed as an argument to the built-in function iter() , it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator.
по простому на русском (c) @jfs:
».join() принимает составной объект—коллекцию строк, такую как список, которую можно в for-цикл передать, чтобы все строки обойти
Вот один из способов определения является ли объект итерируемым и заодно примеры итерируемых объектов:
How to Solve Python TypeError: can only join an iterable
The join() takes all items in an iterable and joins them into one string. If you attempt to pass a non-iterable object to the join() method, you will raise the error: Python TypeError: can only join an iterable.
You can solve this by ensuring that you do not assign the result of any method that performs in-place to a variable with the same name as the iterable you want to join. If you do this, you will pass a None object to the join() method, which is non-iterable.
This tutorial will go through the error in detail and an example to learn how to solve it.
Table of contents
TypeError: can only join an iterable
TypeError tells us that we are trying to perform an illegal operation for a specific Python data type.
There are data types like NoneType that are non-iterable. As the error message suggests, join() is an illegal operation for non-iterable objects.
Example: Sorting a List of Strings
Let’s look at an example where we define a list of names we want to join. However, we want to sort the names in ascending order using the sort() method before joining. The code would look as follows:
Let’s run the code to see the result:
Our program throws the error because the sort() method performs in-place sorting and returns a None value. If we assign the output of names.sort() to names , the variable will be None when we pass it to the join() method. None is a non-iterable data type, and join() is illegal on non-iterable objects.
Solution
To solve the problem, we must keep the names variable as a list, not a None object. We do not need to assign the output of the names.sort() to a variable, and we can call the method and sort the list in place. Let’s look at the revised code:
Let’s run the code to see what happens:
Our code successfully sorts the list of names, joins them with an empty-space separator, and prints the result to the console.
Summary
Congratulations on reading to the end of this tutorial! If you attempt to pass a non-iterable object like a None to the <span lang:python decode:true">join()</span> method, you will raise the error: Python TypeError: can only join an iterable. This error commonly occurs when you use a function that performs and assigns the result to the variable you want to join. If a function performs in place and returns a None value, you do not need to assign it to a variable, for example, sort() . To solve this error, ensure you are using the function without assignment.
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.
Python TypeError: can only join an iterable Solution

With the help of a Python string join() method, we can concatenate all the string values of an iterable object to a new string. The join() method accepts the iterable object as an argument value and concatenates the strings present in the iterable object.
But if we pass a non-iterable object to the join() method, we will encounter the TypeError: can only join an iterable Error.
In this Python guide, we will discuss this error in detail and see how to debug it in a Python program. We will also walk through an example to understand this error in detail.
So let’s get started with the error statement.
Python Error: TypeError: can only join an iterable
Python string is a sequence of characters, and if we wish to concatenate an iterable object of string values as a single list object, we can use the string join() method.
The string join() method accepts an iterable object of string values as an argument and joins all the string values of the iterable with the separator string value.
Syntax
Example
The join() method only accepts an iterable object as an argument value, and if we pass a not iterable object like int , float , and function to the join method, it will raise the TypeError: can only join an iterable Error.
The Error statement TypeError: can only join an iterable has two parts Exception Type and an Error message.
- TypeError (Exception Type)
- can only join an iterable (Error Message)
1. TypeError
The TypeError is one of the Python standard exceptions, and it is raised in a Python program when we specify an inappropriate operation on a Python object or pass an invalid data type argument to a function or method.
2. can only join an iterable
The » can only join an iterable » statement is an Error Statement that tags along the Python’s TypeError. This error message is raised in a Python program when passing a non-iterable data object to the join method. The join method can only join an iterable object of string values, and if we pass a non-iterable object to a join method, it will throw the TypeError with the «can only join an iterable» message.
Common Example Scenario
Let’s say we have a list of students’ Names, and we wish to print all the students’ names as a single string value after sorting the student names in ascending order.
To sort the student name lists, we use the list sort method, and to print all the students’ names as a single string value, we can join all the students’ names using the join() method.
Example Error.
Output
Break The code
In the above example, we are getting the error in line 8 with » all_students = ‘-‘.join(students) » Statement.
This is because in line 8, the value of students was None . In line 5, when we sort the students list using students.sort() method and assign the value to the students, it made the students value to None.
The list sort() method perform the in-place sorting and return a None value. And when we assign the value to students.sort() to students in line 5, the value of students became None and when we pass that None value to the join () method raised the Error.
Solution
To solve the above problem, we need to ensure that the students value we are passing to the join() method must be a list, not a None object. And when we talk about the above problem, there we do not need to assign the students.sort() return value to the students . The sort() method will sort the students list in place, and we can pass that to the join() method.
Output
Conclusion
The «TypeError: can only join an iterable» error is one of the most common errors you would encounter while working with converting a list of string values to a string. This Error is raised in a program when we assign a non-iterable value to the join method. So whenever you encounter this error in your program, you need to ensure that the value you are trying to pass to the join() method is an iterable object at that part of the program.
If you are still getting this error in your Python program, please share your code and query in the comment section. We will try to help you in debugging.
Python String join() function

In this article, we will have a look at the Python String join() function. As the name suggests, it is used to join strings together and works for the data of string type.
Understanding Python string join() method
Python String has various in-built functions to deal with the string type of data.
The join() method basically is used to join the input string by another set of separator/string elements. It accepts iterables such as set, list, tuple, string, etc and another string(separable element) as parameters.
The join() function returns a string that joins the elements of the iterable with the separator string passed as an argument to the function.