# How To Resolve TypeError: ‘Function’ Object Is Not Iterable In Python

While programming, you have probably encountered the TypeError: ‘function’ object is not iterable in Python. In this article, I will help you know the cause and two solutions to fix it: put a () in the function you want to call or iterate on an iterable object. Maybe the article will help you.

## What causes the TypeError: ‘function’ object is not iterable error?

In Python, intervals are objects that allow you to iterate over their elements with a for a loop. Intertable objects don’t need an index, and the length doesn’t even need to be finite. They all have in common that they all contain more than one element. For example, list, string, and tuple.

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type.

The TypeError: ‘function’ object is not iterable happens because you are iterating over a function and not an iterable object.

Example:

def greeting():
myStr = 'hello'
return myStr

for i in greeting:
print(greeting())

Output:

Traceback (most recent call last):
File "code.py", line 5, in <module>
for i in greeting:
TypeError: 'function' object is not iterable

I’m trying to iterate over a function. The problem is on line 6. I have a function called ‘greeting’. It will return a string. To get that string, you need to work with a greeting(); otherwise, you are dealing with the function itself, not the string you want to return.

## How to solve the TypeError: ‘function’ object is not iterable error in Python?

### Put the () at the end of your function call

As stated above, you need to manipulate greeting(). It will return a string from that function.

Example:

• Initialize a function that returns a string.
• Manipulate the function by placing () at the end of the function you want to call. This will iterate over the string returned by the function.
def greeting():
myStr = 'hello'
return myStr

print('String when looped:')

# Put () at the end of the called function
for i in greeting():
print(greeting())

Output:

String when looped:
hello
hello
hello
hello
hello

### Iterate over an iterable object

To make it simpler, why would you use iterable objects that allow you to iterate over its elements with a for loop like a list, string, or tuple.

Example:

• Initialize iterable objects.
• Iterate over the elements of an iterable object with a for loop.
# Initialize a list
myList = ['visit', 'learnshareit']

print('Repeated element in the list:')
for i in myList:
print(i)

# Initialize a string
myString = 'python'

print('Repeated element in the string:')
for i in myString:
print(i)

Output:

Repeated element in the list:
visit
learnshareit
Repeated element in the string:
p
y
t
h
o
n

## Summary

This is all I have to say about the TypeError: ‘function’ object is not iterable in Python. You can iterable on an iterable object so that no function declaration is required. If there is a part that is not reasonable, not convincing enough, and I hope to understand, we will try to improve our article.

Maybe you are interested: