How to Solve Python TypeError: ‘numpy.int64’ object is not iterable

by | Programming, Python, Tips

This error occurs when you try to iterate over a numpy.int64 object, for example, using a for loop.

You can solve this error by passing it to the range() method to get an iterable to iterate over. For example,

import numpy as np

arr = np.array([3, 7, 8, 4, 9], dtype=int)

min_val = min(arr)

for val in range(int(min_val)):

    print(val)

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: ‘numpy.int64’ object is not iterable

TypeError occurs in Python when you perform an illegal operation for a specific data type. A numpy.int64 is the 64-bit integer number type, and we cannot iterate over it.

What is an Iterable Object in Python?

An iterable is an object that can be “iterated over“, for example in a for loop. In terms of dunder methods under the hood, an object can be iterated over with “for” if it implements __iter__() or __getitem__().

An iterator returns the next value in the iterable object. An iterable generates an iterator when it is passed to the iter() method.

In terms of dunder methods under the hood, an iterator is an object that implements the __next__() method.

A for loop automatically calls the iter() method to get an iterator and then calls next over and over until it reaches the end of the iterable object.

We can verify that __iter__ is not an attribute of numpy.int64 class using the dir() method. For example,

import numpy as np

arr = np.array([3, 7, 8, 4, 9], dtype=int)

min_val = min(arr)

print(type(min_val))

print('__iter__' in dir(min_val))
<class 'numpy.int64'>
False

Example #1

Let’s look at an example of trying to iterate over a numpy.int64 object.

First, we will define an array of numpy.int64 values.

import numpy as np

arr = np.array([2, 3, 1, 0, 7, 8], dtype=int)

Next, we will iterate over the numpy array and pass each value in the array to the built-in method max().

for val in arr:

    print(max(val))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [60], in <cell line: 1>()
      1 for val in arr:
----> 3     print(max(val))

TypeError: 'numpy.int64' object is not iterable

The error occurs because the max() method requires an iterable object with one or more items to compare. We pass a numpy.int64 to the max() method with each iteration in the for loop.

Solution

We can solve the error by using a two-dimensional array instead of a one-dimensional array. Each item in a two-dimensional array is an array. Therefore, we can iterate over the two-dimensional array and pass each item to the max() method call. Let’s look at the revised code:

import numpy as np

arr = np.array([[2, 3, 1], [10, 7, 8]], dtype=int)

for val in arr:

    print(max(val))

Let’s run the code to see the result:

3
10

We successfully calculated the maximum value in the two arrays.

Example #2

Let’s look at another example of trying to iterate over a numpy.int64 object.

import numpy as np

arr = np.array([3, 7, 8, 4, 9], dtype=int)

for val in max(arr):

    print(val)

In the above code, we defined an array containing numpy.int64 values and then tried to iterate over the maximum value of the array. Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [62], in <cell line: 5>()
      1 import numpy as np
      3 arr = np.array([3, 7, 8, 4, 9], dtype=int)
----> 5 for val in max(arr):
      7     print(val)

TypeError: 'numpy.int64' object is not iterable

The error occurs because the max() method call returns a numpy.int64 object, which is not iterable.

Solution

We can solve this error by passing the numpy.int64 object to the range() method. The range() method returns a range object, which is an iterable consisting of a sequence of integers.

Let’s look at the revised code:

import numpy as np

arr = np.array([3, 7, 8, 4, 9], dtype=int)

max_val = max(arr)

for val in range(max_val):

    print(val)

Let’s run the code to get the result:

0
1
2
3
4
5
6
7
8

Summary

Congratulations on reading to the end of this tutorial!

For more reading on not iterable TypeErrors, go to the article:

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

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!