Can not save an element in zip into a varible

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(names,insurance_costs)

num_medical_records = len(list(zip(names,insurance_costs)))

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

first_medical_record = medical_records.[0]

It generate error here:

File “”, line 20*
first_medical_record = medical_records.[0]*
SyntaxError: invalid syntax*

Thanks for your help!

You don’t need the . between medical_records and the list index ([0]).

Additionally, (by that line) medical_records still stores a zip object, which cannot have item accessed by index. In order to access the first tuple in medical_records, you’d need to cast it to a list before trying to access any indices.

1 Like

I correct it into:

first_medical_record = list(medical_records)[0]

but it still generate error here:

Traceback (most recent call last):
File “”, line 20, in
first_medical_record = list(medical_records)[0]
IndexError: list index out of range

Hmm; yes; I appear to be getting that. You may need to cast the variable to a list on a different line to the one you are getting an index from it…

= list(medical_records)[0]


It’s like that right?

Not quite; that would throw an error, since you’re trying to assign list(…) on a line with nothing on the left of the equal sign. I would suggest casting to list on one line (with assignment), and grabbing and assigning the first index on a different line.

I think it is not a problem. I suddenly remember that zip does not return value into variable so I use it directly

first_medical_record = list(zip(names,insurance_costs))[0]

instead of:

first_medical_record = list(medical_records)[0]

And it generate the result

If you want to do one task (say adding three numbers) in two lines, you could so something like:

a = 3 + 3
a = a + 4
#output: 10
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.