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

Hi! I’m getting this error: TypeError: object of type ‘zip’ has no len()

This is for question no. 4 from n Working with Python Lists: Medical Insurance Project. Can someone tell me why I keep getting this error?

My code is here: https://gist.github.com/ec72f1aa68772c18bada105d9a4b7e22

1 Like
medical_records = zip(names, insurance_costs)
print(list(medical_records))

#The length of medical records
num_medical_records = len(medical_records)

ZIP objects do not have a len attribute. That last line triggers this error.


>>> len(zip([1, 2, 3], [4, 5, 6]))
Traceback (most recent call last):
  File "<pyshell#223>", line 1, in <module>
    len(zip([1, 2, 3], [4, 5, 6]))
TypeError: object of type 'zip' has no len()
>>> 
2 Likes

Can you elaborate on this? I am having the same issue as the person who originally asked this question and my code was identical. Thanks!!

#The length of medical records
num_medical_records = len(medical_records)

In the above, medical_records is a zip object, and since it has no len() (meaning we cannot measure its length) calling on that function with the object as the argument will raise an error. It is a TypeError because the function was called on an invalid type (zip).

Casting the zip object to a list, or tuple, will allow us to determine the length since those two types are valid arguments.

1 Like

zip() it’s a data type itself and we can’t use len() on it. what you can do is type cast your zip() variable into a list! then you’ll be able to use len() on it.