# Python Lists Medical Insurance Project - Sorting?

Hello Friends -

I’m working on the Python Lists Medical Project, and I’ve gotten to the part where we’re sorting the 2d list into numerical order. I’m getting stuck with the sort() function. I’ve done exactly what the hints say but still not getting the sorted result seen in the hint. Halp!
Code:

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]

names.append(“Priscilla”)
insurance_costs.append(8320.0)

medical_records = list(zip(names, insurance_costs))

print(medical_records)

num_medical_records = len(medical_records)

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

first_medical_record = medical_records[0]
print("Here is the first medical record: " + str(first_medical_record))

#sorting starts here

medical_records.sort()
print("Here are the medical records sorted by insurance cost: " + str(medical_records))

Take another look at the order of your lists here.

Also, here’s the documentation on that built in method.

2 Likes

I’m having this issue as well. My list is being sorted alphabetically instead of numerically (with the lowest cost appearing first), which is what the assignment is asking for. The hint tells us to use the sort function, but it is not sorting in the order they are asking for.

Python documentation on the sort method says a key specifies a function of one argument that is used to extract a comparison key from each list element. I was attempting to make the key the variable called insurance_costs by which the list would be sorted, but that is not working either. I’m wondering if I should be using the sorted function instead.

``````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]

names.append("Priscilla")
insurance_costs.append(8320.0)
medical_records = zip(names, insurance_costs)
medical_records = list(medical_records)

print(medical_records)

num_medical_records = len(medical_records)
print("There are " + str(num_medical_records) + " medical records.")

first_medical_record = medical_records[0]
print("Here is the first medical record: " +
str(first_medical_record))

medical_records.sort()
print("Here are the medical records sorted by insurance cost: " + str(medical_records))
``````

My output looks like this:

``````Here are the medical records sorted by insurance cost:
[('Aaron', 7640.0), ('Emily', 6072.0), ('Jide', 5360.0), ('Mohamed', 13262.0), ('Nikita', 2750.0), ('Paul', 5054.0), ('Paul', 12064.0), ('Priscilla', 8320.0), ('Sara', 4816.0), ('Valentina', 14724.0), ('Xia', 6839.0)]
``````

But Codecademy is telling me that the output for my list should look like this:

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

Does @lisalisaj’s reply not cover this?The order of items in your tuple matters. It’s not strictly ordering by alphabetical order so much as ordering by the first element of each of your tuples. However, no matter which key you sort by you’d never get that expected output whilst zipping those two lists the other way around.

There’s a link in the previous reply but for a more generalised introduction to how sorting proceeds see-
https://docs.python.org/3/howto/sorting.html#sortinghowto

1 Like

No, her reply didn’t explain all the detail you just mentioned in your response, which helped me better understand what exactly I needed to change and why.

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