Last modified: Apr 23, 2026 By Alexander Williams

Fix NoneType Not Iterable Error

Python is a powerful language, but it can sometimes surprise beginners with cryptic errors. One common error is the TypeError: argument of type 'NoneType' is not iterable. This error usually appears when you try to loop over or check membership on a variable that is None instead of a list, string, or dictionary.

This guide will explain what causes this error, how to reproduce it, and how to fix it. We will also explore best practices to avoid it in your future projects. By the end, you will understand how to debug this issue quickly and keep your Python code running smoothly.

What Does This Error Mean?

The error message tells you that Python expects an iterable object, like a list or a string, but it found None. In Python, None is a special value representing the absence of a value. It is not a collection, so you cannot iterate over it or use in to check membership.

For example, if you write for item in my_var and my_var is None, Python raises this error. This often happens when a function returns None instead of the expected data.

Common Causes of NoneType in Iteration

There are a few typical scenarios where this error occurs. Understanding them will help you spot the issue faster.

1. Function Returns None: Many Python functions implicitly return None if you forget to include a return statement. For example, a function that modifies a list in place and does not return it will give you None.

2. Incorrect Method Usage: Some methods like list.sort() or dict.update() modify the object in place and return None. If you assign the result to a variable, that variable becomes None.

3. Missing Data: When reading from files, databases, or APIs, a lookup might return None if the key or record does not exist. Using that result directly in a loop will trigger the error.

If you encounter other Python errors, you might find our full guide on Python TypeError: Causes and Fixes helpful for a broader understanding.

Example 1: Function Returning None

Let's look at a simple example where a function is supposed to return a list but does not.


# Function that forgets to return the list
def get_numbers():
    numbers = [1, 2, 3]
    # No return statement

result = get_numbers()
print(result)  # Output: None

# Trying to iterate over None
for num in result:
    print(num)

When you run this code, you will see the error because result is None. The function get_numbers() does not have a return statement, so it returns None by default.


None
Traceback (most recent call last):
  File "example.py", line 9, in 
    for num in result:
TypeError: argument of type 'NoneType' is not iterable

Solution: Add a return statement to the function.


def get_numbers():
    numbers = [1, 2, 3]
    return numbers  # Now returns the list

result = get_numbers()
for num in result:
    print(num)  # Works fine

Now the output will be the numbers 1, 2, and 3, no error.

Example 2: Using sort() Incorrectly

Another common mistake is assigning the result of list.sort() to a variable. The sort() method sorts the list in place and returns None.


my_list = [3, 1, 2]
sorted_list = my_list.sort()  # sort() returns None
print(sorted_list)  # Output: None

# Trying to iterate over sorted_list
for item in sorted_list:
    print(item)

The sort() method modifies my_list directly, but sorted_list becomes None. Iterating over it causes the error.


None
Traceback (most recent call last):
  File "example2.py", line 6, in 
    for item in sorted_list:
TypeError: argument of type 'NoneType' is not iterable

Solution: Use the sorted() built-in function instead, which returns a new list.


my_list = [3, 1, 2]
sorted_list = sorted(my_list)  # Returns a new list
print(sorted_list)  # Output: [1, 2, 3]

for item in sorted_list:
    print(item)  # Works fine

Example 3: Dictionary Lookup Returning None

When you use the get() method on a dictionary and the key does not exist, it returns None by default. If you try to iterate over that result, the error appears.


data = {"name": "Alice", "age": 30}
hobbies = data.get("hobbies")  # Key 'hobbies' does not exist
print(hobbies)  # Output: None

# Trying to iterate over None
for hobby in hobbies:
    print(hobby)

None
Traceback (most recent call last):
  File "example3.py", line 6, in 
    for hobby in hobbies:
TypeError: argument of type 'NoneType' is not iterable

Solution: Provide a default empty list to the get() method, or check if the result is None before iterating.


data = {"name": "Alice", "age": 30}
hobbies = data.get("hobbies", [])  # Returns [] if key missing
print(hobbies)  # Output: []

for hobby in hobbies:
    print(hobby)  # No error, loops over empty list

How to Fix the Error in Your Code

When you see this error, follow these steps to resolve it:

Step 1: Identify the Variable — Look at the line where the error occurs. The variable causing the issue is the one you are iterating over or using with in.

Step 2: Check the Variable Value — Add a print() statement before the loop to see what the variable contains. If it prints None, you have found the culprit.

Step 3: Trace Back the Source — Determine where that variable came from. Was it returned from a function? Assign from a method? Retrieved from a dictionary?

Step 4: Add a Guard Clause — Before iterating, check if the variable is None and handle it gracefully.


result = get_data()
if result is None:
    print("No data available")
else:
    for item in result:
        print(item)

This approach prevents the error and makes your code more robust.

Best Practices to Avoid This Error

Prevention is better than debugging. Here are some habits that will help you avoid this error in the future.

Always return values from functions. Make sure every function that is supposed to produce a result has a return statement. If a function is complex, consider using type hints to clarify what it should return.

Use sorted() instead of sort() when you need a new list. This avoids confusion with in-place modifications. The sorted() function always returns a list.

Provide default values for dictionary lookups. Use the get() method with a default argument, like data.get("key", []), to ensure you always get an iterable.

Check for None before iteration. A simple if var is not None: guard can save you from many headaches. This is especially important when working with external data sources.

For more advanced debugging techniques, you can refer to our article on Python TypeError: Causes and Fixes which covers similar issues in depth.

Conclusion

The TypeError: argument of type 'NoneType' is not iterable is a common Python error that occurs when you try to iterate over a None value. It usually stems from a function that returns None, an in-place method that does not return a value, or a missing key in a dictionary.

By understanding the root causes and applying the solutions shown in this article, you can fix this error quickly. Always check your variables with print(), use guard clauses, and follow best practices like returning values explicitly and providing defaults for lookups.

With these tools, you will write cleaner, more reliable Python code. Happy coding!