How to Solve Python TypeError: object of type ‘zip’ has no len()

by | Programming, Python, Tips

This error occurs when you try to pass a zip object to a len() method call. The zip() function takes iterables and aggregates them into a tuple. The resultant zip object is an iterator of tuples. In Python, iterators do not have a length.

You can solve this error by converting the zip object to a list object using the built-in list() method. Then you can pass the list as the argument to the len() method. For example,

a = ("Apple", "Mango", "Guava")
b = ("Spinach", "Carrot", "Potato")

x = list(zip(a, b))

print(len(x))

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: object of type ‘zip’ has no len()

We raise a Python TypeError when attempting to perform an illegal operation for a specific type. In this case, the type is zip.

The part ‘has no len()‘ tells us the zip object does not have a length.

Therefore len() is an illegal operation for the zip object.

Retrieving the length of an object is only suitable for iterable objects, like a list or a tuple. A zip object is an iterator of tuples where the items in the passed iterators are paired together in order.

The len() method implicitly calls the dunder method __len__() which returns a positive integer representing the length of the object on which it is called. All iterable objects have __len__ as an attribute. Let’s check if __len__ is in the list of attributes for the zip object and the list object using the built-in dir() method.

a = ("Apple", "Mango", "Guava")
b = ("Spinach", "Carrot", "Potato")

x = zip(a, b)

# Get the type of the object

print(type(x))

print('__len__' in dir(x))
<class 'zip'>
False

We can see that __len__ is not present in the attributes of the zip object.

lst = ["Spinach", "Carrot", "Potato"]

print(type(lst))

print('__len__' in dir(lst))
<class 'list'>
True

We can see that __len__ is present in the attributes of the list object.

Example

Let’s look at an example of trying to get the length of a zip object. In the following code, we will define two tuples, containing four strings each. Then we will use the built-in zip method to pair the elements of the tuples together into a zip object. Finally, we will attempt to get the length of the zip object.

a = ("Jill", "Xavier", "Lucy", "Janice")
b = ("Chance", "Will", "Ken", "Harold")

x = zip(a, b)

print(len(x))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 6>()
      2 b = ("Chance", "Will", "Ken", "Harold")
      4 x = zip(a, b)
----> 6 print(len(x))

TypeError: object of type 'zip' has no len()

The error occurs because the zip object is an iterator and does not have a length.

Solution

We can solve the error by converting the zip object to a list, which is an iterable data type. We can convert a zip object to a list using the built-in list() method. Let’s look at the revised code:

a = ("Jill", "Xavier", "Lucy", "Janice")
b = ("Chance", "Will", "Ken", "Harold")

x = list(zip(a, b))

print(len(x))

Let’s run the code to get the length of the list.

4

We successfully converted the zip object to a list and then used the len() method to get the length of the list.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on the has no len() TypeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python, which provides the best, easy-to-use online courses.

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 ✨