Last modified: Apr 23, 2026 By Alexander Williams

Fix: cannot unpack non-iterable NoneType

Have you ever seen this error in your Python code?


TypeError: cannot unpack non-iterable NoneType object

This error is common for beginners. It happens when you try to unpack a value that is None. Python cannot split None into multiple variables. In this article, you will learn what causes this error and how to fix it.

What Does This Error Mean?

In Python, unpacking means assigning values from an iterable to multiple variables. For example:

 
# This works fine
a, b = [1, 2]
print(a)  # Output: 1
print(b)  # Output: 2

But if you try to unpack something that is not iterable, like None, Python raises the TypeError.

 
# This will cause the error
a, b = None  # TypeError: cannot unpack non-iterable NoneType object

The error says "non-iterable NoneType". NoneType is the type of None. None is not a list, tuple, or any iterable. So Python cannot unpack it.

Common Causes of This Error

1. Function Returns None

Many Python functions do not return anything. If a function does not have a return statement, it returns None by default. If you try to unpack the result, you get this error.

 
def get_values():
    # No return statement
    x = 10
    y = 20

# This will cause the error
a, b = get_values()  # TypeError

To fix this, make sure your function returns an iterable.

 
def get_values():
    return 10, 20  # Returns a tuple

a, b = get_values()
print(a, b)  # Output: 10 20

2. Method Returns None

Some built-in methods return None. For example, .sort() modifies a list in place and returns None. If you try to unpack it, you get the error.

 
my_list = [3, 1, 2]
# Wrong: .sort() returns None
a, b, c = my_list.sort()  # TypeError

Instead, use the sorted() function which returns a new sorted list.

 
my_list = [3, 1, 2]
a, b, c = sorted(my_list)
print(a, b, c)  # Output: 1 2 3

3. Missing Return in Conditional

Sometimes a function returns a value only in some cases. If a condition is not met, the function returns None.

 
def divide(a, b):
    if b != 0:
        return a / b
    # No return for b == 0, so returns None

result = divide(10, 0)  # result is None
# Unpacking will fail

Always handle all possible paths in your function.

 
def divide(a, b):
    if b != 0:
        return a / b
    else:
        return None  # Explicitly return None or handle error

result = divide(10, 0)
if result is not None:
    print(result)
else:
    print("Cannot divide by zero")

How to Debug the Error

When you see this error, follow these steps:

  1. Check the function return value. Print the result before unpacking.
  2. Use print() statements to see what your function returns.
  3. Check if the variable is None before unpacking.
 
def get_data():
    # Some code that may return None
    return None

result = get_data()
print(type(result))  # Output: 

# Now you know it's None, handle it
if result is not None:
    a, b = result
else:
    print("Result is None, cannot unpack")

Preventing the Error

You can use type checking or default values to prevent this error.

Use Type Hints and Checks

Type hints can help you catch issues early. But you still need runtime checks.

 
def get_coordinates() -> tuple:
    # This hint says it returns a tuple, but it might return None
    # Always ensure the function returns the promised type
    return (10, 20)

x, y = get_coordinates()  # Works fine

Use Default Values

If a function might return None, provide a default iterable.

 
def get_values():
    # Some logic that might fail
    return None

# Use a default tuple
result = get_values() or (0, 0)
a, b = result
print(a, b)  # Output: 0 0

Real-World Example

Imagine you are reading data from a file. If the file is empty, the function might return None.

 
def read_data(filename):
    try:
        with open(filename) as f:
            line = f.readline()
            if line:
                return line.strip().split(",")
            else:
                return None  # File is empty
    except FileNotFoundError:
        return None

data = read_data("missing.txt")
if data is not None:
    name, age = data
else:
    print("No data to unpack")

This pattern is safe. It checks for None before unpacking.

Related Python Error

If you often see this error, you might also encounter other type errors. Check our guide on Python TypeError: Causes and Fixes for more help.

Conclusion

The TypeError: cannot unpack non-iterable NoneType object happens when you try to unpack None. Always ensure your functions return an iterable. Use print() to debug. Check for None before unpacking. With these tips, you can fix and prevent this error in your Python code.

Remember: never unpack a value you haven't verified. Always check if the value is iterable before unpacking. This simple habit will save you from many frustrating bugs.