How to resolve TypeError: ‘list’ object is not an iterator in Python

If you are looking for a solution to the TypeError: ‘list’ object is not an iterator error in Python, then this is the article for you. The cause of the error is that the list is used as iterator and I will fix it by passing the list. book to iter method or use generator. Post details below.

What causes the TypeError: ‘list’ object is not an iterator error?

Iterator are objects that allow us to get each element in it, which can be done iteratively. For example, using a loop over an iterator.

In Python, the iterator implements two particular protocols, the __iter__ protocol and the __next__ protocol.

  • The __iter__ method returns the iterator object itself. The method must be implemented if you want to use the for loop or the print command on the iterator object.
  • The __next__ method returns the next element. If the iterator runs out of elements, the program will throw a StopIteration error.

The TypeError: ‘list’ object is not an iterator error that happens because the list is used as an iterator. 

Example

listCities = ['Frankfurt', 'Amsterdam', 'Venice']

# Using the __next__ method causes an error
print(next(listInfor))
print(next(listInfor))
print(next(listInfor))

Output:

Traceback (most recent call last):
 File "./prog.py", line 4, in <module>
TypeError: 'list' object is not an iterator

How to solve this error?

Pass the list to the __iter__ method.

Example:

  • Pass the list to the __iter__ method to get the iterator object itself. 
  • The result is an iterator, this time using the __next__ method to return the next elements.
listCities = ['Frankfurt', 'Amsterdam', 'Venice']

# The __iter__ method returns the iterator object itself
iteratorObj = iter(listCities)

# Use the __next__ method to get elements in the list
print(next(iteratorObj))
print(next(iteratorObj))
print(next(iteratorObj))

Output:

Frankfurt
Amsterdam
Venice

One problem when you use the next() method is that when the iterator runs out of program elements, it will throw a StopIteration error. To prevent the program from stopping, use the try/except block.

Example:

listCities = ['Frankfurt', 'Amsterdam', 'Venice']

# The __iter__ method returns the iterator object itself
iteratorObj = iter(listCities)

try:
    # Use the __next__ method to get elements in the list
    print(next(iteratorObj))
    print(next(iteratorObj))
    print(next(iteratorObj))
    print(next(iteratorObj))
except StopIteration:
    print('Out of return value')

Output:

Frankfurt
Amsterdam
Venice
Out of return value

Use generator.

The generator is a simple way to create iterators. Without a generator, creating an iterator goes through quite a few steps, such as implementing the class with __iter()__ and __next()__ methods, keeping track of the internal conditions, keeping track of the StopIteration that occurs when no value is returned. Simplified, a Generator is a function that returns an iterator object that we can iterate over. In creating a Generator, you still use the ‘def’ keyword like a function, but instead of ‘return’ as a function, the generator uses the ‘yield’ statement to return the elements.

Example:

listCities = ['Frankfurt', 'Amsterdam', 'Venice']

def myGenerator():
    for i in range(len(listCities)):
        yield listCities[i]

gen = myGenerator()

try:
    print(next(gen))
    print(next(gen))
    print(next(gen))
    print(next(gen))
except StopIteration:
    print('Out of return value')

Output:

Frankfurt
Amsterdam
Venice
Out of return value

Summary

The problem TypeError: ‘list’ object is not an iterator in Python is probably solved. If you have any questions or have a more creative way, I hope you will share it with everyone by commenting below. Thank you for reading my article.

Leave a Reply

Your email address will not be published. Required fields are marked *