Using Python's max to return two equally large values
Solution 1:
Idea is to find max value and get all keys corresponding to that value:
count = {'a': 120, 'b': 120, 'c': 100}
highest = max(count.values())
print([k for k, v in count.items() if v == highest])
Solution 2:
Same idea as Asterisk, but without iterating over the list twice. Bit more verbose.
count = { 'a': 120, 'b': 120, 'c': 100 }
answers = []
highest = -1
def f(x):
global highest, answers
if count[x] > highest:
highest = count[x]
answers = [x]
elif count[x] == highest:
answers.append(x)
map(f, count.keys())
print answers
Solution 3:
Fast single pass:
a = { 'a': 120, 'b': 120, 'c': 100 }
z = [0]
while a:
key, value = a.popitem()
if value > z[0]:
z = [value,[key]]
elif value == z[0]:
z[1].append(key)
print z
#output:
[120, ['a', 'b']]
And an amusing way with defaultdict:
import collections
b = collections.defaultdict(list)
for key, value in a.iteritems():
b[value].append(key)
print max(b.items())
#output:
(120, ['a', 'b'])