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.

2 Likes

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.

I try it, but it return wrong result. Instead of return 11, it return 0. Why?

Immediately after creating the zip, cast it to a list. Casting a list will consume the zip object, so be sure to assign the list to a variable.

names.append('Priscilla')
insurance_costs.append(8320.0)
medical_records = zip(names, insurance_costs)
print(list(medical_records))
num_medical_records = len(list(medical_records))
print('There are ' + str(num_medical_records) + ' medical records.')

Here is my code and it got wrong result.

Be sure to assign to a variable, before printing. What that line is doing is consuming the zip object, but not saving it, only printing it. The list vanishes after that.

a = list(medical_records)
print (a)

Now the list is persistent and accessible.

3 Likes

Wow! I made it! Thanks a lot!

1 Like

I followed the advice above and i am still getting 0 medical records returning.

my code is below: what am i still doing wrong?

names.append(“Priscilla”)

insurance_costs.append(8320.0)

medical_records = zip(names, insurance_costs)

a = list(medical_records)

print(a)

num_medical_records = len(list(medical_records))

print(“There are " + str(num_medical_records) + " medical records.”)


When you make the assignment,

a = list(medical_records)

you consume the zip object assigned to the medical_records variable. But, you have already saved the result to the a variable.

# You wrote:
a = list(medical_records)
print(a)
num_medical_records = len(list(medical_records))

# It should be:
a = list(medical_records)
print(a)
num_medical_records = len(a)

# You could also have done:
medical_records = list(medical_records)
print(medical_records)
num_medical_records = len(medical_records)
2 Likes

ooooooh. thank you for responding and clarifying! i got it now!

1 Like