Select Page

How to Solve Python TypeError: ‘int’ object is not iterable

by | Programming, Python, Tips

Integers and iterables are distinct objects in Python. An integer stores a whole number value, and an iterable is an object capable of returning elements one at a time, for example, a list. If you try to iterate over an integer value, you will raise the error “TypeError: ‘int’ object is not iterable”. You must use an iterable when defining a for loop, for example, range(). If you use a function that requires an iterable, for example, sum(), use an iterable object as the function argument, not integer values.

This tutorial will go through the error in detail. We will go through two example scenarios and learn how to solve the error.


TypeError: ‘int’ object is not iterable

TypeError occurs in Python when you try to perform an illegal operation for a specific data type. For example, if you try to index a floating-point number, you will raise the error: “TypeError: ‘float’ object is not subscriptable“. The part ‘int’ object is not iterable tells us the TypeError is specific to iteration. You cannot iterate over an object that is not iterable. In this case, an integer, or a floating-point number.

An iterable is a Python object that you can use as a sequence. You can go to the next item in the sequence using the next() method.

Let’s look at an example of an iterable by defining a dictionary:

d = {"two": 2, "four":4, "six": 6, "eight": 8, "ten": 10}

iterable = d.keys()

print(iterable)
dict_keys(['two', 'four', 'six', 'eight', 'ten'])

The output is the dictionary keys, which we can iterate over. You can loop over the items and get the values using a for loop:

for item in iterable:

    print(d[item])

Here you use item as the index for the key in the dictionary. The following result will print to the console:

2
4
6
8
10

You can also create an iterator to use the next() method

d = {"two": 2, "four":4, "six": 6, "eight": 8, "ten": 10} 

iterable = d.keys()

iterator = iter(iterable)

print(next(iterator))

print(next(iterator))
two

four

The code returns the first and second items in the dictionary. Let’s look at examples of trying to iterate over an integer, which raises the error: TypeError: ‘int’ object is not iterable”.

Example #1: Incorrect Use of a For Loop

Let’s consider a for loop where we define a variable n and assign it the value of 10. We use n as the number of loops to perform: We print each iteration as an integer in each loop.

n = 10

for i in n:

    print(i)
TypeError                                 Traceback (most recent call last)
      1 for i in n:
      2     print(i)
      3 

TypeError: 'int' object is not iterable

You raise the error because for loops are used to go across sequences. Python interpreter expects to see an iterable object, and integers are not iterable, as they cannot return items in a sequence.

Solution

You can use the range() function to solve this problem, which takes three arguments.

range(start, stop, step)

Where start is the first number from which the loop will begin, stop is the number at which the loop will end and step is how big of a jump to take from one iteration to the next. By default, start has a value of 0, and step has a value of 1. The stop parameter is compulsory.

n = 10

for i in range(n):

   print(i)
0
1
2
3
4
5
6
7
8
9

The code runs successfully and prints each value in the range of 0 to 9 to the console.

Example #2: Invalid Sum Argument

The sum() function returns an integer value and takes at most two arguments. The first argument must be an iterable object, and the second argument is optional and is the first number to start adding. If you do not use a second argument, the sum function will add to 0. Let’s try to use the sum() function with two integers:

x = 2

y = 6

print(sum(x, y))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 print(sum(x,y))

TypeError: 'int' object is not iterable

The code is unsuccessful because the first argument is an integer and is not iterable.

Solution

You can put our integers inside an iterable object to solve this error. Let’s put the two integers in a list, a tuple, a set and a dictionary and then use the sum() function.

x = 2

y = 6

tuple_sum = (x, y)

list_sum = [x, y]

set_sum = {x, y}

dict_sum = {x: 0, y: 1}

print(sum(tuple_sum))

print(sum(list_sum))

print(sum(set_sum))

print(sum(dict_sum))
8
8
8
8

The code successfully runs. The result remains the same whether we use a tuple, list, set or dictionary. By default, the sum() function sums the key values.

Summary

Congratulations on reading to the end of this tutorial. The error “TypeError: ‘int’ object is not iterable” occurs when you try to iterate over an integer. If you define a for loop, you can use the range() function with the integer value you want to use as the stop argument. If you use a function that requires an iterable, such as the sum() function, you must put your integers inside an iterable object like a list or a tuple.

For further reading on the ‘not iterable’ TypeError go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!