Home > Mobile >  How to split dictionary keys and change the type in python
How to split dictionary keys and change the type in python

Time:04-18

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?

CodePudding user response:

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}
  • Related