TypeError: 'function' object is not iterable (Fix)

Fix the Python error TypeError: 'function' object is not iterable. This error usually means Python expected something it could loop over, but you gave it a function itself instead of the value returned by that function.

In most cases, the fix is simple: call the function with parentheses.

Quick fix #

def get_numbers():
    return [1, 2, 3]

for n in get_numbers():
    print(n)

This error often happens when you loop over a function name instead of the value returned by the function. Add parentheses if you meant to call the function.

What this error means #

Python expected an iterable object, such as:

  • a list
  • a tuple
  • a string
  • a set
  • a dictionary
  • a range

But it got a function object instead.

A function is something you call to get a result. It is not something you can loop over directly.

If you are not fully comfortable with functions yet, see Python functions explained and iterators and iterable objects explained.

Why this happens #

This error usually appears when:

  • You wrote a function name without parentheses
  • You used a function in a for loop
  • You used a function with list(), tuple(), set(), sum(), any(), all(), or similar code that expects an iterable
  • You returned a function instead of returning data by mistake

Common example that causes the error #

Here is a simple example:

def get_numbers():
    return [1, 2, 3]

for n in get_numbers:
    print(n)

Output:

TypeError: 'function' object is not iterable
Traceback (most recent call last):File "example.py", line 4, in <module>for n in get_numbers:TypeError: 'function' object is not iterableWhere it happened — file and lineWhat went wrong — the exception typeWhy — the detailed message
Read bottom-up: get_numbers without () is the function itself; add parentheses to loop over its result.

Why this fails #

  • get_numbers is the function itself
  • get_numbers() is the list returned by the function

Python cannot loop over the function object, but it can loop over the list returned by the function.

Fix 1: Call the function #

If you want the function’s result, add parentheses.

def get_numbers():
    return [1, 2, 3]

for n in get_numbers():
    print(n)

Output:

1
2
3

This is the most common fix.

Use:

  • my_function() if you want the returned value
  • my_function only when you want to refer to the function itself

Fix 2: Check what the function returns #

Sometimes you already called the function, but the returned value still is not iterable.

For example:

def get_total():
    return 10

for n in get_total():
    print(n)

This also fails, because 10 is an integer, and integers are not iterable.

How to debug it #

Check the returned value and its type:

def get_total():
    return 10

value = get_total()
print(value)
print(type(value))

Output:

10
<class 'int'>

If the function returns None, int, or bool, you still cannot iterate over it.

A function must return something iterable, such as a list or tuple:

def get_total_parts():
    return [3, 7]

for n in get_total_parts():
    print(n)

If you need help checking types, see Python type() explained.

Fix 3: Do not overwrite iterable variables with functions #

Sometimes the problem is caused by reusing the same name for different things.

Example:

items = [1, 2, 3]

def items():
    return [4, 5, 6]

for x in items:
    print(x)

This fails because items now refers to the function, not the original list.

Better version #

Use different names:

items = [1, 2, 3]

def get_items():
    return [4, 5, 6]

for x in items:
    print(x)

Or, if you want the function result:

def get_items():
    return [4, 5, 6]

for x in get_items():
    print(x)

Clear names help prevent this kind of mistake.

Where this error appears #

You may see this error in places like these:

In a for loop #

def names():
    return ["Ana", "Ben"]

for name in names:
    print(name)

With the in operator #

def colors():
    return ["red", "blue"]

print("red" in colors)

With list(), tuple(), or set() #

def letters():
    return "abc"

print(list(letters))

With sum() #

def numbers():
    return [1, 2, 3]

print(sum(numbers))

In all of these cases, the fix is usually to call the function:

print("red" in colors())
print(list(letters()))
print(sum(numbers()))

Beginner debugging steps #

When you see this error, use these steps:

  1. Find the exact line shown in the traceback
  2. Check whether you used a function name without parentheses
  3. Print the object and its type before the failing line
  4. Confirm that the value is actually iterable

Useful debugging code:

print(my_function)
print(type(my_function))

value = my_function()
print(value)
print(type(value))

Example #

def get_numbers():
    return [1, 2, 3]

print(get_numbers)
print(type(get_numbers))

value = get_numbers()
print(value)
print(type(value))

Output:

<function get_numbers at 0x...>
<class 'function'>
[1, 2, 3]
<class 'list'>

That makes the problem easier to spot:

  • get_numbers is a function
  • get_numbers() is a list

If you want a step-by-step process, see the beginner guide to debugging Python code.

Common mistakes #

These are the most common causes of this error:

  • Using for x in my_function instead of for x in my_function()
  • Passing a function to list() or set() by mistake
  • Forgetting to call a function before using the in operator
  • Returning a function object instead of returned data
  • Reusing the same name for both data and a function

Here is one more example of returning a function by mistake:

def get_numbers():
    return [1, 2, 3]

def wrapper():
    return get_numbers

for n in wrapper():
    print(n)

This fails because wrapper() returns the function get_numbers, not the list.

Correct version #

def get_numbers():
    return [1, 2, 3]

def wrapper():
    return get_numbers()

for n in wrapper():
    print(n)

FAQ #

Why does Python say a function is not iterable? #

Because a function is something you call with parentheses. It is not a collection of values you can loop over.

How do I fix this error quickly? #

Check whether you forgot parentheses. If you meant to use the function result, change my_function to my_function().

Can a function ever be iterable? #

Not by default. Normally you call a function to get a value, and then you iterate over that returned value if it is iterable.

What if I already called the function and still get an error? #

Then the returned value may not be iterable. Check the return value and its type.

If the returned value is None, you may also want to read TypeError: 'NoneType' object is not iterable. Similar problems can happen with TypeError: 'int' object is not iterable and TypeError: 'bool' object is not iterable.

See also #

Press Esc to close