In Python, you can unpack iterable objects and assign their elements to multiple variables in the order they appear. If you try to unpack a numpy.float64
, you will throw the error TypeError: cannot unpack non-iterable numpy.float64
object. A numpy float is not a sequence which we can loop over.
You can solve this error by unpacking an iterable object like a list or a tuple. For example,
import numpy as np arr = np.array([3.2, 7.5, 8.6, 4.5, 9.0]) a, b, c, d, e = arr
This tutorial will go through how to solve the error with code examples.
Table of contents
TypeError: cannot unpack non-iterable numpy.float64 object
TypeError occurs in Python when you perform an illegal operation for a specific data type. numpy.float64 is the numpy-specific 64-bit float type. Floating-point numbers are not iterable objects. Unpacking is only suitable for iterable objects.
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()
function.
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.
Unpacking requires an iteration in order to assign values to variables in order, and as such requires iterable objects.
What is Unpacking in Python?
Unpacking is the process of splitting packed values into individual elements. The packed values can be a string, list, tuple, set or dictionary. During unpacking, the elements on the right-hand side of the statement are split into the values on the left-hand side based on their relative positions. Let’s look at the unpacking syntax with an example:
values = [10, 20, 30] x, y, z = values print(f'x: {x}, y: {y}, z: {z}')
The above code assigns the integer values in the value list to three separate variables. The value of x
is 10
, y
is 20
, and the value of z
is 30
. Let’s run the code to get the result:
x: 10, y: 20, z: 30
We can also unpack sets and dictionaries. Dictionaries are only ordered for Python version 3.7 and above but are unordered for 3.6 and below. Generally, it is not recommended to unpack unordered collections of elements as there is no guarantee of the order of the unpacked elements.
We cannot unpack an Integer because it is not an iterable object, and an iterable is a Python object that we can iterate over.
Example
Let’s look at an example of attempting to unpack a numpy.float64
object. First, we will define a function that returns a numpy.float64
.
import numpy as np def return_np_float(): return np.float64(4.56)
Next, we will try to unpack the object returned by the function and assign three values to the variables named x
, y
, and z
.
x, y, z = return_np_float() print(f'x: {x}, y: {y}, z: {z}')
Let’s run the code to see what happens.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [3], in <cell line: 1>() ----> 1 x, y, z = return_np_float() 3 print(f'x: {x}, y: {y}, z: {z}') TypeError: cannot unpack non-iterable numpy.float64 object
The error occurs because the function returns a numpy.float64
, and we are attempting to do an unpack operation, which is not possible with floating-point numbers.
We can use the type()
function to check the type of an object. Let’s verify the type of the object returned by the return_np_float()
function call:
print(type(return_np_float()))
<class 'numpy.float64'>
Solution
We can solve this error by ensuring the function we call returns an iterable object. We will return a numpy array containing three floating-point values in this case. Let’s look at the revised code:
import numpy as np def return_np_floats(): return np.array([4.56, 3.74, 9.12])
Next, we will call the return_np_floats()
function to return the array and unpack it.
x, y, z = return_np_floats() print(f'x: {x}, y: {y}, z: {z}')
Let’s run the code to see the result:
x: 4.56, y: 3.74, z: 9.12
We successfully called the function and unpacked the numpy.ndarray
into three variables, and printed their values to the console.
Summary
Congratulations on reading to the end of this tutorial!
For more reading on cannot unpack non-iterable object errors, go to the articles:
- How to Solve Python TypeError: cannot unpack non-iterable NoneType object
- How to Solve Python TypeError: cannot unpack non-iterable float object
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.