TypeError: Iteration Over Non-Sequence

TypeError: Iteration Over Non-Sequence

  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

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.

Related Article - Python TypeError

  • TypeError: Must Be Real Number, Not STR
  • Solve the TypeError: An Integer Is Required in Python
  • Python TypeError: Unhashable Type: List
  • Solve the TypeError: Not All Arguments Converted During String Formatting in Python
  • Python TypeError: Missing 1 Required Positional Argument
  • Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10