Last modified: Apr 24, 2026 By Alexander Williams
Fix TypeError: Cannot Unpack Non-iterable int
Python is a powerful language. But beginners often face confusing errors. One common error is TypeError: cannot unpack non-iterable int object. This error stops your code. It can be frustrating. But don't worry. This guide explains why it happens. It shows you how to fix it. You will learn with simple examples. By the end, you will solve this error easily.
What Does This Error Mean?
Python lets you unpack values from iterables. An iterable is something you can loop over. Lists, tuples, strings, and dictionaries are iterables. An integer is not iterable. You cannot loop over a single number. When you try to unpack an integer, Python throws this error. It says you cannot unpack a non-iterable int object.
Unpacking means assigning multiple variables at once. For example, a, b = [1, 2] works. But a, b = 5 does not. Python expects an iterable on the right side. It gets an integer instead. This causes the error.
Common Scenarios That Cause This Error
Let's look at typical code that triggers this error. Understanding these helps you avoid it.
1. Unpacking a Single Integer
The simplest case is unpacking a number. You might write code like this:
# This causes the error
x, y = 42
Output:
TypeError: cannot unpack non-iterable int object
The number 42 is not a list or tuple. Python cannot split it into two parts. You must provide an iterable.
2. Function Returning an Integer Instead of a Tuple
Functions often return multiple values as tuples. If a function returns only one integer, unpacking fails. Consider this:
# Function returns a single int
def get_data():
return 100
# Trying to unpack it
a, b = get_data()
Output:
TypeError: cannot unpack non-iterable int object
The function get_data() returns an integer. You try to unpack it into two variables. This causes the error. The fix is to return a tuple or list.
3. Misunderstanding Loop Variables
A common mistake happens in loops. You might write:
# Wrong loop unpacking
numbers = [1, 2, 3]
for x, y in numbers:
print(x, y)
Output:
TypeError: cannot unpack non-iterable int object
Here, numbers is a list of integers. Each item is a single integer. The loop tries to unpack each integer into two variables. This fails. You need each item to be an iterable, like a tuple.
How to Fix the Error
Now you know the causes. Let's fix them. The solution depends on your situation. Here are the most effective fixes.
Fix 1: Provide an Iterable
If you want to assign multiple variables, use a list or tuple. For example:
# Correct unpacking
x, y = [42, 43] # list works
a, b = (100, 200) # tuple works
print(x, y) # Output: 42 43
print(a, b) # Output: 100 200
Output:
42 43
100 200
Always check the right side is an iterable. If it is a single value, assign it to one variable only.
Fix 2: Return a Tuple from Functions
When a function should return multiple values, return a tuple. Use parentheses or commas. Here's the corrected version:
# Fixed function returns tuple
def get_data():
return 100, 200 # comma creates tuple
a, b = get_data()
print(a, b) # Output: 100 200
Output:
100 200
Now the function returns two values. Unpacking works perfectly.
Fix 3: Correct Loop Unpacking
For loops, ensure each item is iterable. Use a list of tuples or lists. Example:
# Correct loop with tuples
pairs = [(1, 2), (3, 4), (5, 6)]
for x, y in pairs:
print(x, y)
Output:
1 2
3 4
5 6
Each element in pairs is a tuple. Python unpacks it into x and y. No error occurs.
Fix 4: Check Your Data Type
Sometimes you get an integer unexpectedly. Use type() to check. Then adjust your code. For example:
# Check type before unpacking
value = some_function()
print(type(value)) # Might show int
# If it's an int, don't unpack
if isinstance(value, int):
result = value
else:
a, b = value
This prevents the error. You handle both cases safely.
Preventing the Error in the Future
Good habits reduce errors. Always verify function return values. Use type hints in your code. They make intentions clear. For example:
# Using type hints
def get_data() -> tuple[int, int]:
return 100, 200
a, b = get_data() # Safe now
Type hints help you and others understand what a function returns. They catch errors early. Also, test your code with small examples. This builds confidence.
Another tip: use Python's built-in enumerate() correctly. It returns tuples. You can unpack them safely. For example:
# enumerate returns (index, value) tuples
items = ['a', 'b', 'c']
for i, item in enumerate(items):
print(i, item)
Output:
0 a
1 b
2 c
This works because enumerate() yields tuples. Unpacking is natural here.
Related Errors to Know
This error is part of a family of TypeError issues. You might also see "cannot unpack non-iterable NoneType object" or "cannot unpack non-iterable float object". The logic is the same. The right side must be iterable. For a deeper look at similar errors, check our guide on Python TypeError: Causes and Fixes. It covers many common mistakes.
Conclusion
The TypeError: cannot unpack non-iterable int object is easy to fix. It happens when you try to unpack an integer. Always ensure the right side of an assignment is iterable. Use lists, tuples, or other iterables. Check function return values. Correct loop structures. With these steps, you can avoid this error. Practice with the examples above. Soon, you will spot and fix it quickly. Happy coding!