I am trying to write code to find the mean of the keys in my dict, but based on the dict values. So, for example, for:
d = {1:2, 2:1, 3:2}
the dict keys would be:
[1,1,2,3,3]
I've written the following code, which works for small data sets such as the above:
def get_median_of_dict_keys(d: dict) -> float:
nums_list = []
for k,v in d.items():
if type(v) != int:
raise TypeError
nums_list.extend([k] * v)
median = sum(nums_list) / len(nums_list)
return median
This gets me the values I want when the data set is small, but if the data set is something like:
d = {1:1_000_000_000_000_000, 2:2_000, 3:1_000_000_000_000_000}
I get an out of memory error which, now that I think about it, makes sense.
So how can I structure the above function in a way that will also handle those larger data sets? Thanks for your time.