Finding and sorting by 2nd dimension in a list ("Working with Python Lists: Medical Insurance Costs Project")

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

Hello,

In the mentioned link, python lists medical insurance costs project, we are asked to create medical records 2D list where the price is the first dimension, and names are in the second one.

Later we’re asked to count the amount of times patient named Paul is listed in the records.

Is there anyway to scan the 2nd dimension using lst.count() method?

Same with lst.sort()? Can’t I sort it based on the 2nd dimension?

Thanks!

Well, first you’d use .zip(),

medical_records = list(zip(insurance_costs, names))

Yes, you’d use .count()
ie:
occurrences_paul = names.count("Paul")

Do you have any (formatted) code to post?

For .sort() See:
https://docs.python.org/3/howto/sorting.html

1 Like
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(insurance_costs, names)) medical_records.sort() #sorts by insurance cost occurrences_paul = medical_records.count("Paul") print("There are " + str(occurrences_paul) + " individuals with the name Paul in our medical records.")

Thanks for taking interest.
I have the mentioned snippet attached.

It doesn’t seem to find paul in the patients list.

Thanks!

You’re using the wrong list.

The directions state: " Count the number of occurrences of “Paul” in the names list, and store the result in a variable called occurrences_paul ."

Yes I know the requested list is the names list, but I was wondering can I search the 2D list. I.e., search the data in its 2nd dimension.

I see from the link you sent about sorting that you can in fact sort by higher dimensions.

You might’ve realised but 2D lists in Python can be a pain to work with, I’d avoid the nesting if you can. I don’t think there’s a high-level way to get at this data, you basically have to loop through and access the second element which prevents you from using .count() directly.

There might be tricks to make this more convenient with map to access elements or zip to transpose the data but at the end of the day it’s the same task (they’re tools you probably won’t come across for a while but if you’re curious you can search around for similar problems).

1 Like

Thanks a lot!

Does the definition for .count() uses a simple for statement that loops through the list?
Because in this situation there’s really no reason why not to create a function like def count_nD(lst, dimension): that just nest loops to get search access to higher dimensions in my opinion.

Yes you could certainly write your own function for this task which would probably be far more convenient in the future should you require it again.

In standard CPython at least the answer for does the .count method use loops is technically yes it uses a loop but for built-in types like a list the methods are actually implemented in C so there’s no simple way to access this implementation.

Implementation details ahead, possibly interesting but probably not useful to you...

If you’re very curious the source code can be found here- https://github.com/python/cpython/blob/3.10/Objects/listobject.c (search for list.count since it’s on line 2500+ as of CPython3.10).

Long story short not matter how you accessed it you’d first need to look up each individual nested list object from the first outer list and then look up the 2nd element from that inner list object. There’s no direct connection otherwise.

1 Like