Last modified: Apr 25, 2026 By Alexander Williams
Fix TypeError: Cannot Unpack Float
Python's unpacking feature is powerful but can trip you up. The TypeError: cannot unpack non-iterable float object occurs when you try to assign a single float value to multiple variables. This error is common for beginners. Let's break it down simply.
Unpacking means taking an iterable (like a list or tuple) and assigning its elements to separate variables. A float is not iterable. You cannot split a single number into parts without explicit logic.
What Does the Error Mean?
Python expects an iterable on the right side of an assignment when you use multiple variables on the left. A float is a single value, not a sequence. So Python raises this error.
For example, you might accidentally return a float from a function instead of a tuple. Or you might try to unpack a single calculation result. The fix is to ensure the right side is always iterable.
# This will cause the error
x, y = 3.14 # TypeError: cannot unpack non-iterable float object
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot unpack non-iterable float object
Common Scenarios and Fixes
1. Accidentally Returning a Float Instead of a Tuple
A function might return a single float when you expect two values. Always check your return statements. Use a tuple or list to return multiple values.
# Problematic function
def get_coordinates():
return 5.0 # Only returns a float, not a tuple
# This will fail
# x, y = get_coordinates()
# Fixed version
def get_coordinates():
return (5.0, 10.0) # Returns a tuple
x, y = get_coordinates()
print(x, y) # Output: 5.0 10.0
5.0 10.0
2. Unpacking a Single Calculation Result
If you compute a value and try to unpack it directly, you get this error. Store the result first, or ensure the calculation returns an iterable.
# Wrong way
# a, b = 10 + 5 # This is a float, not iterable
# Right way
a, b = [10, 5] # Use a list or tuple
print(a, b) # Output: 10 5
# Another fix: unpack after calculation
result = 10 + 5 # This is a single float
# Do not unpack it
10 5
3. Using a Loop That Returns a Single Value
A loop might yield a single float instead of a pair. Check your loop structure. Use zip or enumerate to pair values.
# Problematic loop
data = [1.0, 2.0, 3.0]
for x, y in data: # Each item is a single float, not a pair
print(x, y) # Error
# Fixed loop
data = [(1.0, 2.0), (3.0, 4.0)]
for x, y in data:
print(x, y) # Works fine
1.0 2.0
3.0 4.0
How to Debug This Error
First, check the line number in the traceback. Look at the right side of the assignment. Print its type using type() to confirm it's a float.
# Debugging example
value = some_function()
print(type(value)) # Check if it's a float
# If float, fix the function to return an iterable
Second, review your function returns. Ensure every return path gives an iterable. Use tuples consistently. For more help with similar errors, check our guide on Python TypeError: Causes and Fixes.
Best Practices to Avoid This Error
Always test your functions with sample inputs. Use type hints to enforce return types. For example, specify -> tuple[float, float] in your function definition.
When you write loops, verify that the iterable contains pairs. Use zip to combine two lists into pairs. This prevents accidental unpacking of single floats.
# Safe unpacking with zip
x_coords = [1.0, 2.0, 3.0]
y_coords = [4.0, 5.0, 6.0]
for x, y in zip(x_coords, y_coords):
print(x, y) # Always works
1.0 4.0
2.0 5.0
3.0 6.0
Related Python Errors
This error is similar to TypeError: cannot unpack non-iterable int object. The same principles apply. Always ensure the right side is a list, tuple, or other iterable. For a deeper dive into Python type errors, see our article on Python TypeError: Causes and Fixes.
Conclusion
The TypeError: cannot unpack non-iterable float object is easy to fix once you understand unpacking. Always check that the value on the right side of an assignment is iterable. Use tuples for multiple return values. Test your functions with debug prints. With these practices, you'll avoid this error in your Python code. Remember, a float is a single number, not a sequence. Keep your data structures consistent, and unpacking will work smoothly.