TypeError: 'int' object is not iterable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: 'int' object is not iterable in Python #

The Python "TypeError: 'int' object is not iterable" occurs when we try to iterate over an integer or pass an integer to a built-in function like, list() or tuple(). To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(10):.

typeerror int object is not iterable

Here is an example of how the error occurs.

main.py
num = 10 # ⛔️ TypeError: 'int' object is not iterable for i in num: print(i)

We are trying to iterate over an integer, but integer objects are not iterable.

We can use the range() built-in function to iterate over a range.

main.py
num = 10 for i in range(num): print(i) # 👉️ 0, 1, 2, ... 8, 9

The range function is commonly used for looping a specific number of times in for loops and takes the following parameters:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

If values for the start and stop parameters are provided, the start value is inclusive, whereas the stop value is exclusive.

main.py
num = 5 for i in range(1, num): print(i) # 👉️ 1, 2, 3, 4
Another common cause of the error is passing an integer to the built-in constructors, e.g. list(), dict(), tuple() and set().

The following 4 calls to the built-in constructors cause the error.

main.py
num = 10 # ⛔️ TypeError: 'int' object is not iterable list(num) dict(num) tuple(num) set(num)

To solve the error, we have to correct the assignment and figure out where the integer value is coming from.

Here are working examples of using the 4 built-ins.

main.py
l = list(['a', 'b', 'c']) print(l) # 👉️ ['a', 'b', 'c'] d = dict(name='Alice', age=30) print(d) # 👉️ {'name': 'Alice', 'age': 30} t = tuple([1, 2, 3]) print(t) # 👉️ (1, 2, 3) s = set(['a', 'b', 'a']) print(s) # 👉️ {'a', 'b'}

You have to figure out where the integer value came from and correct the assignment.

The error is commonly caused when using the len() built-in function.

main.py
my_str = 'hello' # ⛔️ TypeError: 'int' object is not iterable for i in len(my_str): print(i)

The len function returns an integer that represents the length of the sequence.

If you need to iterate that many times, you can pass the result to the range() function.

main.py
my_str = 'hello' for i in range(len(my_str)): print(i) # 👉️ 0, 1, 2, 3, 4

If you need to iterate with both the index and the current character or element, use the enumerate() function.

main.py
my_str = 'hello' for idx, c in enumerate(my_str): print(idx, c) # 👉️ 0 h, 1 e, 2 l... my_list = ['a', 'b', 'c'] for idx, el in enumerate(my_list): print(idx, el) # 👉️ 0 a, 1 b, 2 c

If you need to check if an object is iterable, use a try/except statement.

main.py
my_str = 'hello' try: my_iterator = iter(my_str) for i in my_iterator: print(i) # 👉️ h, e, l, l, o except TypeError as te: print(te)

The iter() function raises a TypeError if the passed in value doesn't support the __iter__() method or the sequence protocol (the __getitem__() method).

If we pass a non-iterable object like an integer to the iter() function, the except block is run.

main.py
my_int = 100 try: my_iterator = iter(my_int) for i in my_iterator: print(i) except TypeError as te: print(te) # 👉️ 'int' object is not iterable

Examples of iterables include all sequence types (list, str, tuple) and some non-sequence types like dict, file objects and other objects that define an __iter__() or a __getitem__() method.

Conclusion #

The Python "TypeError: 'int' object is not iterable" occurs when we try to iterate over an integer or pass an integer to a built-in function like, list() or tuple(). To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(10):.

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.