how do i find mode in dictionary . if keys are ‘certain values’ and values of dictionary are frequency of ‘certain value’ like below dictionary?
{45: 2, 50: 3, 56: 1, 66: 2, 67: 2, 70: 2}
how do i find mode in dictionary . if keys are ‘certain values’ and values of dictionary are frequency of ‘certain value’ like below dictionary?
{45: 2, 50: 3, 56: 1, 66: 2, 67: 2, 70: 2}
d = {45: 2, 50: 3, 56: 1, 66: 2, 67: 2, 70: 2}
#create a list of keys in dictionary
key_list = list(d.keys())
# create a list of value in dictionary
value_list = list(d.values())
# both key list and value list will have same ordering i.e. if key_list = [45,50,56] then value_list = [2,3,1]
# so now find the index in value_list with maximum values
max_value = max(value_list)
index_max = value_list.index(max_value)
# whatever is the key in key_list at this index_max that becomes the mode
print(key_list[index_max])
i have updated the above code please try again
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.