Working with Python Lists: Medical Insurance Project

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

I’m stuck with this part, where len() doesn’t show length. I’ve tried it here:


and it works!
So why I get 0 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

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(num_medical_records )

if you want to use the len() method, just do (you don’t need list:

num_medical_records = len(medical_records)

More on .len():
https://www.geeksforgeeks.org/find-size-of-a-ist-in-python/

Use list when you want to print out the results of .zip.
ex:

drinks = ['Manhattan', 'Whisky Sour', 'Spicy Margarita']
prices = [10, 9, 8]

drink_menu = list(zip(drinks, prices))
print(drink_menu)

output: [('Manhattan', 10), ('Whisky Sour', 9), ('Spicy Margarita', 8)]

num_medical_records = len(medical_records)

Tried that:

Traceback (most recent call last):
  File "script.py", line 13, in <module>
    num_medical_records = len(medical_records)
TypeError: object of type 'zip' has no len()

This is the issue.
(I think it’s also in the hint in the lesson)

A zip object is not a list, so, you cannot use the len() function. You have to cast the zip object as a list and then you can use len()
More on this here:
https://www.dotnetperls.com/zip-python

And the CC cheatsheet is here:
https://www.codecademy.com/learn/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/cheatsheet

1 Like

great I did convert to list but the result was 0, see my first post

list is not being used correctly. The way that medical_records is defined is incorrect. You have to cast the zip object as a list first. You don’t use list when you print() nor when you define num_medical_records.

Look at my drinks and prices example above. When you use zip, to combine two lists, what happens is that it creates tuples (which aren’t lists), so, you get a zip object which you need to convert (or cast) to a list.

1 Like

ok, i figured out: here’s solution

medical_records = list(zip(names, insurance_costs ))

1 Like

Yes! That’s correct. :slight_smile:

Once you’ve created the medical_records variable, use print()

print() is our friend…seriously. That’s an excellent way to check your code.

thanks! if you forgot some part, it’s hard to go back and find it and cheatsheet isn’t always help

I hear you. There’s definitely a LOT to remember…with Python or any programming language.
That’s why it’s good to turn to sites like geeksforgeeks, StackOverflow, RealPython, etc. Google is our friend too. :slight_smile:
I also find that taking notes as I go along helps (so I can explain concepts in my own words) and looking at the official documentation for python.