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!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨