Help with using sorted in python pls

Hello, I am trying to sort a list by the second element index[1] of the tuple but it’s not working. in this example. i want to sort by insurance cost instead of name

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

medical_records= list(zip(names, insurance_costs))

#print(medical_records)

sorted(medical_records, key=lambda record:record[1])

print("Here are the medical records sorted by insurance cost: " + str(medical_records))
Thank you in advance!

The sorted function does not act on the existing list. It creates a new one. You can either use the .sort() method of the existing list or you can work with what sorted returns.

I am stuck on the same step of the same exercise. I tried both of the following, but it gives a type error. My second solution is actually exactly what is suggested in the “Hint” for that step.

medical_records = list(zip(insurance_costs, names))
medical_records_sorted = sorted(medical_records)

OR
medical_records = list(zip(insurance_costs, names))
medical_records.sort()

Thanks again!

It’s worth having a quick nose through- How to ask good questions (and get good answers), particularly the bits about code formatting and including your code and the error message.

At present you mention a type error but not what line it came from and without more of the code it’s impossible to guess why. A little description goes a long way :slightly_smiling_face:.

1 Like

Thanks for your patience – I’ll take a look!

1 Like

Trying again! This is the same exercise OP was talking about –
https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/projects/ds-python-lists2-project

Here is the code that was provided at the start of the exercise, as well as all the lines I wrote before I got the error:

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

At this point, the instructions in the exercise ask you to sort medical_records

I tried a couple of ways, including the suggestion in the hint:

medical_records.sort()
medical_records_sorted = sorted(medical_records)

Both produced the same type error:

TypeError: '<' not supported between instances of 'str' and 'float'

In the end, I could finish the exercise with an unsorted list, but I would like to know why it didn’t work. Thanks!

1 Like

Thanks, that’s ample detail :grinning:.

You’ll catch an error like that with something like [3, "3"].sort(). The sorting algorithm relies on certain operations that aren’t supported between certain different types in Python, for example: 3 < "3".

This is a bit more complex with the tuples you have but as per- Sorting HOW TO — Python 3.9.5 documentation you’re sorting by the first element of each tuple at first. So at some point a string and an int must be compared. Can you spot where?

Hint
# Try the following to see if you can spot the odd one out-
print(insurance_costs)
2 Likes

Yay, thanks! I found it – I had appended an integer as a string. Now it works. Really appreciate your patient help :grinning_face_with_smiling_eyes:

1 Like

Hi, I have a similar issue… I understand that I have to use a key in the sort() function and so I am able to sort the list by the insurance cost.

However, the exercise shows that insurance cost should now come before name - since I zipped the original two lists with the name first and insurance cost second, is there a way to reverse the order within each nested list with some funky code other than just me reversing the order in the original zip()? If so, what terminology should I be Googling?

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 = 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_records = medical_records[0] print("Here is the first medical record: " + str(first_medical_records)) medical_records.sort(key = lambda x: x[1]) print("Here are the medical records sorted by insurance cost: " + str(medical_records))

Thank you in advance!

Ok - I think think the video about Tuples, that followed, explained that I can’t change the order through code because it’s immutable. My only option is to change the order when I zip().