TypeError: 'numpy.float64' object is not iterable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: 'numpy.float64' object is not iterable in Python #

The Python "TypeError: 'numpy.float64' object is not iterable" occurs when we try to iterate over a numpy float or pass a float to a built-in function like list() or tuple(). To solve the error, iterate over an array or another iterable or use the range() function.

typeerror numpy float64 object is not iterable

Here is an example of how the error occurs.

main.py
import numpy as np arr = np.array([1.1, 2.2, 3.3]) print(max(arr)) # 👉️ 3.3 # ⛔️ TypeError: 'numpy.float64' object is not iterable for i in max(arr): print(i)

We used the max function to get the max value in the array and try to iterate over it, but floats are not iterable.

You can iterate over the array:

main.py
import numpy as np arr = np.array([1.1, 2.2, 3.3]) for i in arr: print(i) # 👉️ 1.1, 2.2, 3.3

The same approach can be used if you have a two-dimensional array.

main.py
import numpy as np arr = np.array([[1.1, 2.2], [3.3, 4.4]]) for a in arr: print(a) # 👉️ [1.1, 2.2] [3.3, 4.4] print(max(a)) # 👉️ 2.2, 4.4

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

main.py
import numpy as np arr = np.array([1.1, 2.2, 3.3, 4.4]) for i in range(int(arr[2])): print(i) # 👉️ 0, 1, 2

Notice that we also used the int() function to convert the float to an integer because the range function expects an integer argument.

Another common cause of the error is passing a float to the built-in constructors or other built-in functions, e.g. list(), dict(), tuple() and set().
main.py
import numpy as np arr = np.array([1.1, 2.2, 3.3, 4.4]) # ⛔️ TypeError: 'numpy.float64' object is not iterable list(arr[0])

Some functions (e.g. list, dict) expect and iterable and raise an error when a non-iterable value is passed.

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'}

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 a float to the iter() function, the except block is run.

main.py
import numpy as np arr = np.array([1.1, 2.2, 3.3, 4.4]) try: my_iterator = iter(arr[0]) for i in my_iterator: print(i) except TypeError as te: print(te) # 👉️ 'numpy.float64' 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: 'numpy.float64' object is not iterable" occurs when we try to iterate over a numpy float or pass a float to a built-in function like list() or tuple(). To solve the error, iterate over an array or another iterable or use the range() function.

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.