Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split dictionary keys and change the type in python

I am tasked with figuring out which ‘keys’ in a dictionary are 300 level or above. An example of a dictionary like this would be {‘CSCI 160’: 4, ‘CSCI 330’: 3, ‘MATH 208’: 3, ‘EE 201":4}. How would I go about splitting the keys and converting the numerical portion into an integer?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This code will create a new dictionary with the key and value pairs from the original dictionary, but only if the value is greater than or equal to 300.

filtered_dict = {key: val for key, val in my_dict.items() if int(key.rsplit(' ', 1)[1]) >= 300}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading