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

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Valery Sysoev

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

The Python "TypeError: object of type 'zip' has no len()" occurs when we pass a zip object to the len() function. To solve the error, convert the zip object to a list before getting its length, e.g. len(list(my_zip)).

typeerror object of type zip has no len

Here is an example of how the error occurs.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] my_zip = zip(list_1, list_2) # ⛔️ TypeError: object of type 'zip' has no len() print(len(my_zip))

We can't pass a zip object to the len() function but we can convert it to a list and get the length of the list.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] # ✅ convert to list my_zip = list(zip(list_1, list_2)) print(len(my_zip)) # 👉️ 3

The list class takes an iterable and returns a list object.

Note that passing a zip object to the list class exhausts the iterator.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] my_zip = zip(list_1, list_2) # 👇️ [(1, 'a'), (2, 'b'), (3, 'c')] print(list(my_zip)) # 👇️ [] print(list(my_zip))

So if you convert a zip object to a list, do it directly and not in multiple places.

The zip function iterates over several iterables in parallel and produces tuples with an item from each iterable.

The zip function returns an iterator of tuples.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] my_zip = list(zip(list_1, list_2)) # 👇️ [(1, 'a'), (2, 'b'), (3, 'c')] print(my_zip)

When we pass an object to the len() function, the object's __len__() method is called.

You can use the dir() function to print an object's attributes and look for the __len__ attribute.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] my_zip = zip(list_1, list_2) print(dir(my_zip))

Or you can check using a try/except statement.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] my_zip = zip(list_1, list_2) try: print(my_zip.__len__) except AttributeError: # 👇️ this runs print('object has no attribute __len__')

We try to access the object's __len__ attribute in the try block and if an AttributeError is raised, we know the object doesn't have a __len__ attribute and cannot be passed to the len() function.

The len() function returns the length (the number of items) of an object.

main.py
my_list = ['apple', 'banana', 'kiwi'] result = len(my_list) print(result) # 👉️ 3
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] print(type(list_1)) # 👉️ <class 'list'> print(isinstance(list_1, list)) # 👉️ True my_zip = zip(list_1, list_2) print(type(my_zip)) # 👉️ <class 'zip'> print(isinstance(my_zip, zip)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Use the search field on my Home Page to filter through my more than 1,000 articles.