Working with Python List: Medical Insurance Project

https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/projects/ds-python-lists2-project

Question 4: Create a variable called num
_variable_records that stores the length of medical records
The hint suggests
num_medical_records = len(medical_records)

However, a TypeError: object of type zip has no len()

I tired the following
num_medical_records = len(list(medical_records)

But this results in : 0

Hello @angel-1989, welcome to the forums! Can you please post your code? To format it correctly, see this post.

1 Like
type or paste code here
names = ["Mohamed", "Sara", "Xia", "Paul", "Valentina", "Jide", "Aaron", "Emily", "Nikita", "Paul"]
insurance_costs = [13262.0, 4816.0, 6839.0, 5054.0, 14724.0, 5360.0, 7640.0, 6072.0, 2750.0, 12064.0]

# Add your code here
medical_records = zip(insurance_costs, names)
print(list(medical_records))

#Size of medical records
num_medical_records = len(medical_records)


print("There are " + str(num_medical_records) + "medical records")

Since calling the zip() function on two lists creates a zip object, you need to turn it into a list first (which you are doing with list()). However, when you print

You are effectively printing a copy of medical_records cast to a list; the actual variable medical_records is still a zip object, which you can’t use len() on.

Hi

Thanks for your response. I reviewed the lessons, and I used “+” to combine the lists

names = ["Mohamed", "Sara", "Xia", "Paul", "Valentine", "Jide", "Aaron", "Emily", "Nikita", "Paul"]

insurance_costs = [13262.0, 4816.0, 6839.0, 5054.0, 14724.0, 5360.0, 7640.0, 6072.0, 2750.0, 12064.0]

# Append Priscilla to names and her insurance cost 8320.0

names.append("Priscilla")
insurance_costs.append(8320.0)

# Combine names and insurance costs

medical_records = names + insurance_costs

print(medical_records)

print(len(medical_records))

>> 22

Are you sure that’s what the project wants you to do? I think it wants you to zip the lists so that each “person” is paired with their insurance cost.

1 Like

Agreed the result should be length 11 records (not 22).

However I am still struggling with turning the zip object into a list so I can get the length of the following zip object:

[(13262.0, ‘Mohamed’), (4816.0, ‘Sara’), (6839.0, ‘Xia’), (5054.0, ‘Paul’), (14724.0, ‘Valentina’), (5360.0, ‘Jide’), (7640.0, ‘Aaron’), (6072.0, ‘Emily’), (2750.0, ‘Nikita’), (12064.0, ‘Paul’), (8320.0, ‘Priscilla’)]

Here is my code so far:
ames = [“Mohamed”, “Sara”, “Xia”, “Paul”, “Valentina”, “Jide”, “Aaron”, “Emily”, “Nikita”, “Paul”]

insurance_costs = [13262.0, 4816.0, 6839.0, 5054.0, 14724.0, 5360.0, 7640.0, 6072.0, 2750.0, 12064.0]

names.append(‘Priscilla’)

insurance_costs.append(8320.0)

print(names)

print(insurance_costs)

medical_records = zip(insurance_costs, names)

print(list(medical_records))

num_medical_records = len(list(medical_records))

print(num_medical_records)

That seems like it prints the right thing. What exactly is the issue?