How to Solve Python TypeError: ‘function’ object is not iterable

by | Programming, Python, Tips

This error occurs when you try to iterate over a function object, for example, using a for loop.

If your function returns an iterable object, you can solve the error by adding parentheses () after the function name to call it and return the object. For example,

def get_planets():

    planets = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune']

    return planets

for planet in get_planets():

    print(attribute)

This tutorial will detail the error and how to solve it with code examples.


TypeError: ‘function’ object is not iterable

TypeError occurs in Python when you perform an illegal operation for a specific data type. A function is a block of code which only runs when it is called, and we cannot iterate over it.

What is an Iterable Object in Python?

An iterable is an object that can be “iterated over“, for example, in a for loop. In terms of dunder methods under the hood, an object can be iterated over with “for” if it implements __iter__() or __getitem__().

An iterator returns the next value in the iterable object. An iterable generates an iterator when it is passed to the iter() method.

In terms of dunder methods under the hood, an iterator is an object that implements the __next__() method.

A for loop automatically calls the iter() method to get an iterator and then calls next over and over until it reaches the end of the iterable object.

Example

Let’s look at an example of trying to iterate over a function.

First, we will create a function that returns a list of planets.

def get_planets():

    planets = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune']

    return planets

First, we will create a function that returns a list of planets.

for planet in get_planets:

    print(planet)

Let’s run the code to see the result

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 for planet in get_planets:
      3     print(planet)

TypeError: 'function' object is not iterable

The error occurs because we did not call the get_planets function. Therefore Python interprets the for loop as trying to iterate over the function object, which is not iterable.

Solution

We can solve this error by calling the function get_planets. We can call a function by specifying the function name and putting parentheses after the name. Let’s look at the revised code:

def get_planets():

    planets = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune']

    return planets

for planet in get_planets():

    print(planet)

Let’s run the code to see the result:

mercury
venus
earth
mars
jupiter
saturn
uranus
neptune

We successfully iterated over the list returned by the get_planets() function.

Summary

Congratulations on reading to the end of this tutorial!

For more reading on not iterable TypeErrors, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!