DatSoup
DatSoup

Reputation: 35

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?

Upvotes: 0

Views: 139

Answers (1)

Dieter
Dieter

Reputation: 2659

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}

Upvotes: 1

Related Questions