Last modified: Dec 04, 2025 By Alexander Williams

Fix TypeError: 'NoneType' is Not Iterable

This error is a common Python stumbling block. It occurs when your code tries to iterate over a None value. Iteration means looping over items. Examples include using for loops or the in operator.

Python's None is a special singleton. It represents the absence of a value. It is not a list, tuple, or string. You cannot loop over nothing. The error message is Python's way of telling you this.

Understanding the Error Message

The full error is: "TypeError: argument of type 'NoneType' is not iterable". This points to a line in your code. Something that should be a collection is actually None.

Functions that do not explicitly return a value return None. A common mistake is assuming they return a list or other iterable. Methods like list.sort() modify a list in place. They return None, not the sorted list.

Common Causes and Examples

Let's look at specific scenarios that trigger this error. Each example shows the bug and the fix.

1. Forgetting Return Values

A function may not return what you expect. If it returns None, assigning its result to a variable gives you None.


# Bug: Function returns None implicitly
def process_data(my_list):
    my_list.sort()  # .sort() returns None

result = process_data([3, 1, 2])
for item in result:  # ERROR: result is None
    print(item)

Traceback (most recent call last):
  File "script.py", line 6, in 
    for item in result:
TypeError: 'NoneType' object is not iterable

The process_data function returns None. The list.sort() method works in-place. The fix is to return the modified list.


# Fix: Return the list explicitly
def process_data(my_list):
    my_list.sort()
    return my_list  # Now returns the list

result = process_data([3, 1, 2])
for item in result:  # Works: result is [1, 2, 3]
    print(item)

1
2
3

2. Misusing List Methods

Methods like .append(), .sort(), and .reverse() return None. A common error is chaining them or assigning their result.


# Bug: Assigning .append() result
my_list = [1, 2, 3]
new_list = my_list.append(4)  # .append() returns None

if 4 in new_list:  # ERROR: new_list is None
    print("Found")

The variable new_list is assigned None. The in operator cannot check membership in None. The fix is to operate on the original list.


# Fix: Operate on the list directly
my_list = [1, 2, 3]
my_list.append(4)  # Modifies my_list in-place

if 4 in my_list:  # Works: my_list is [1, 2, 3, 4]
    print("Found")

Found

3. Dictionary .get() and Iteration

The dictionary .get() method returns None by default if a key is missing. Trying to iterate over this result causes the error.


# Bug: Iterating over a missing key's value
user_data = {'name': 'Alice', 'age': 30}
tags = user_data.get('tags')  # Key doesn't exist, returns None

for tag in tags:  # ERROR: tags is None
    print(tag)

The key 'tags' is not in the dictionary. .get('tags') returns None. The fix is to provide a default iterable, like an empty list.


# Fix: Provide a default iterable with .get()
user_data = {'name': 'Alice', 'age': 30}
tags = user_data.get('tags', [])  # Default to empty list

for tag in tags:  # Safe: tags is []
    print(tag)     # Loop does nothing, no error

General Debugging Strategy

Follow these steps when you encounter this error.

1. Find the line. The traceback points to the line with the for or in statement.

2. Identify the variable. Find which variable is being iterated over on that line.

3. Trace its value. Work backwards. See where that variable gets its value. Is it from a function call? Is it from a method?

4. Check for None. Ensure the source returns an iterable, not None. Use print() or a debugger to inspect the value.

This process helps with many type errors, like Fix TypeError: 'NoneType' has no len() in Python.

Preventative Coding Practices

Adopt these habits to avoid the error in the first place.

Know your methods. Remember which methods return None. This includes list modifiers and dictionary .update().

Use type hints. They can help clarify what a function returns. They serve as documentation.


from typing import List

def get_sorted_items(items: List[int]) -> List[int]:
    """Returns a new sorted list."""
    return sorted(items)  # sorted() returns a new list

Add explicit checks. Before iterating, check if the variable is not None. This is a defensive programming technique.


data = some_function_that_might_return_none()

if data is not None:
    for item in data:
        process(item)
else:
    print("No data received.")

Similar checks are useful for errors like Fix TypeError: 'type' object is not iterable.

Related TypeErrors

This error is part of a family of TypeError exceptions. They all involve using an object in an unsupported way.

For example, Fix TypeError: Not All Arguments Converted in Python deals with string formatting. Understanding one helps with others.

The core lesson is the same. Python objects have specific types. These types support specific operations. Always ensure you are using the right type for the job.

Conclusion

The "NoneType is not iterable" error is a classic Python mistake. It happens when you try to loop over a None value. The root cause is often a function or method that returns None unexpectedly.

To fix it, trace the variable back to its source. Ensure that source returns an actual iterable like a list or tuple. Use default values with .get() and remember in-place methods.

With practice, you will spot these issues quickly. You will write more robust code that handles None gracefully. Keep coding and learning from these errors.