How to Fix TypeError: Iteration Over Non-Sequence

Rohan Timalsina Feb 02, 2024
  1. Recreate TypeError: iteration over non-sequence in Python
  2. Iterate Over a List to Fix TypeError: iteration over non-sequence in Python
  3. Use __iter__ to Fix TypeError: iteration over non-sequence Error in Python
How to Fix TypeError: Iteration Over Non-Sequence

The TypeError is raised in Python when the function or operation is applied to an object of an inappropriate type. For instance, adding integer and string objects returns TypeError.

The error TypeError: iteration over non-sequence occurs when you try to iterate over an object which is not iterable. This tutorial will teach you to fix TypeError: iteration over non-sequence in Python.

Recreate TypeError: iteration over non-sequence in Python

Now you have understood the cause of TypeError, let’s recreate the iteration over non-sequence error in Python. Running the following script in Python 2 gives TypeError: iteration over non-sequence.

class Book:
    def __init__(self, name, author):
        self.name = name
        self.author = author


class Collection:
    def __init__(self):
        self.read = []

    def add_book(self, name, author):
        self.read.append(Book(name, author))


if __name__ == "__main__":
    Books = Collection()
    Books.add_book("Romeo and Juliet", "William Shakespeare")
    Books.add_book("To Kill a Mockingbird", "Harper Lee")
    for book in Books:
        print(book.name)

Here, we are trying to iterate over the object Books instead of the list read inside it. As a result, it gives TypeError: iteration over non-sequence in Python 2.

Output:

Traceback (most recent call last):
  File "Main.py", line 17, in <module>
    for book in Books:
TypeError: iteration over non-sequence

In Python 3, you will get the following output.

Traceback (most recent call last):
  File "Main.py", line 17, in <module>
    for book in Books:
TypeError: 'Collection' object is not iterable

Iterate Over a List to Fix TypeError: iteration over non-sequence in Python

Since Books is an object, you have to iterate over the list inside the object Books. In this case, it will be for book in Books.read.

class Book:
    def __init__(self, name, author):
        self.name = name
        self.author = author


class Collection:
    def __init__(self):
        self.read = []

    def add_book(self, name, author):
        self.read.append(Book(name, author))


if __name__ == "__main__":
    Books = Collection()
    Books.add_book("Romeo and Juliet", "William Shakespeare")
    Books.add_book("To Kill a Mockingbird", "Harper Lee")
    for book in Books.read:
        print(book.name)

Output:

Romeo and Juliet
To Kill a Mockingbird

Use __iter__ to Fix TypeError: iteration over non-sequence Error in Python

The __iter__ method is used when a container requires an iterator. It should return the iterator object that can iterate over all the objects in the container.

Books is an instance of Collection. You can use the __iter__ method to iterate over the object Books.

class Collection:
    def __iter__(self):
        return iter(self.read)

The following block contains the complete script.

class Book:
    def __init__(self, name, author):
        self.name = name
        self.author = author


class Collection:
    def __init__(self):
        self.read = []

    def add_book(self, name, author):
        self.read.append(Book(name, author))

    def __iter__(self):
        return iter(self.read)


if __name__ == "__main__":
    Books = Collection()
    Books.add_book("Romeo and Juliet", "William Shakespeare")
    Books.add_book("To Kill a Mockingbird", "Harper Lee")
    for book in Books:
        print(book.name)

Output:

Romeo and Juliet
To Kill a Mockingbird

That’s how you can solve the iteration over non-sequence error in Python. We hope you find these solutions helpful.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - Python TypeError

Related Article - Python Error