TypeError: cannot unpack non-iterable object (Fix)
This error happens when Python tries to split one value into multiple variables, but that value is not something Python can unpack.
For example, Python can unpack a tuple like (1, 2) into a and b. But it cannot unpack a single integer like 5.
Quick fix
value = 5
# Wrong:
# a, b = value
# Fix 1: assign to one variable
number = value
# Fix 2: unpack an actual iterable
pair = (1, 2)
a, b = pair
print(a, b)
Output:
1 2
Unpacking only works when the value on the right side is an iterable, such as a tuple, list, or string, and it contains the expected number of items.
What this error means
This error means:
- Python tried to split one value into multiple variables.
- The value was not iterable, so Python could not unpack it.
- Common non-iterable values include:
intfloatboolNone
A very common pattern that causes this is:
a, b = value
This only works if value is something like:
- a tuple:
(1, 2) - a list:
[1, 2] - a string:
"hi"
If value is just one number like 5, Python raises a TypeError.
If you are not familiar with iterables, see iterators and iterable objects explained.
When this error happens
You will usually see this error in one of these situations:
- Using code like
a, b = 10 - Expecting a function to return multiple values, but it returns one value
- Looping with unpacking over items that are not pairs or groups
- Reading data and assuming each item has multiple parts
Common causes include:
- Trying to unpack an integer into multiple variables
- Trying to unpack
Nonereturned from a function - Using a loop that expects pairs but the data contains single values
- Forgetting that a function returns one value, not multiple values
- Mixing up dictionary iteration with dictionary
.items()iteration
Example that causes the error
Here is a simple example:
a, b = 5
This fails because 5 is a single integer, not a list or tuple.
You will get an error like this:
TypeError: cannot unpack non-iterable int object
The important part is the same in all versions of this error:
- Python expected something unpackable
- It got a non-iterable value instead
Fix 1: assign to one variable
If the value is just one item, use one variable.
number = 5
print(number)
Output:
5
Use this fix when you do not actually need unpacking.
Wrong:
a, b = 5
Right:
number = 5
This is the simplest solution when your code only has one value.
Fix 2: return or use an iterable
If you want multiple variables, the value on the right side must be an iterable with the right number of items.
Unpacking a tuple
a, b = (1, 2)
print(a)
print(b)
Output:
1
2
Unpacking a list
a, b = [10, 20]
print(a, b)
Output:
10 20
Returning multiple values from a function
A function that should provide multiple values can return a tuple.
def get_coordinates():
return (3, 7)
x, y = get_coordinates()
print(x, y)
Output:
3 7
If this part is unclear, you may want to read return values in Python functions.
Fix 3: check what a function really returns
Sometimes the unpacking code looks correct, but the function returns something different from what you expected.
For example:
def get_total():
return 100
a, b = get_total()
This raises:
TypeError: cannot unpack non-iterable int object
The function returns one integer, not two values.
Debug it first
Print the result before unpacking it:
def get_total():
return 100
result = get_total()
print(result)
print(type(result))
Output:
100
<class 'int'>
Now the problem is easy to see.
Another common case: returning None
def do_something():
print("Done")
result = do_something()
print(result)
print(type(result))
Output:
Done
None
<class 'NoneType'>
If you then try:
a, b = do_something()
You will get a similar error:
TypeError: cannot unpack non-iterable NoneType object
That usually means the function did not return the values you expected. This is related to TypeError: 'NoneType' object is not iterable.
Fix 4: unpack loop items correctly
This error often happens in loops.
Wrong: unpacking single numbers
data = [1, 2, 3]
for a, b in data:
print(a, b)
Each item in data is a single integer, not a pair. So this fails.
Right: use one loop variable
data = [1, 2, 3]
for item in data:
print(item)
Output:
1
2
3
Right: use pairs if you want unpacking
data = [(1, 2), (3, 4), (5, 6)]
for a, b in data:
print(a, b)
Output:
1 2
3 4
5 6
Dictionary example
A common mistake is iterating over a dictionary like this:
person = {"name": "Ana", "age": 20}
for key, value in person:
print(key, value)
This is wrong because looping over a dictionary normally gives you one key at a time.
The correct version is:
person = {"name": "Ana", "age": 20}
for key, value in person.items():
print(key, value)
Output:
name Ana
age 20
For more help with this pattern, see how to loop through a dictionary in Python.
How to debug this step by step
When you see this error, use this checklist:
- Find the line with unpacking, usually something like:
a, b = value - Print the value before that line.
- Print its type.
- Check whether it is iterable.
- Check whether it contains the number of items you expect.
Useful debugging commands:
print(value)
print(type(value))
print(result)
print(type(result))
print(data)
Example:
value = 5
print(value)
print(type(value))
a, b = value
Output before the error:
5
<class 'int'>
That tells you the problem immediately: value is an integer.
Common related cases
You may see slightly different versions of this error:
TypeError: cannot unpack non-iterable int objectTypeError: cannot unpack non-iterable NoneType objectTypeError: cannot unpack non-iterable float object
These all mean the same core thing:
- Python tried to unpack a value
- The value was not iterable
This is different from these errors:
Those errors happen when the value is iterable, but it has the wrong number of items.
FAQ
What does non-iterable mean in Python?
It means the value cannot be looped over or unpacked item by item. Examples include int, float, bool, and None.
Can I unpack a string in Python?
Yes. A string is iterable, so you can unpack it if it has the expected number of characters.
Example:
a, b = "hi"
print(a, b)
Output:
h i
Why do I get this error from a function call?
Usually because the function returned one value, or returned None, but your code expected multiple values.
How is this different from not enough values to unpack?
This error means the value is not iterable at all.not enough values to unpack means the value is iterable, but it does not contain enough items.