-2

Had to re-write the question due to changed requirements.

I have a dictionary such as the following:

d = {'a': [4, 2], 'b': [3, 4], 'c': [4, 3], 'd': [4, 3], 'e': [4], 'f': [4], 'g': [4]}

I want to get the keys that are associated with the smallest length in the dictionary d, as well as those that have the maximum value.

In this case, the keys with the smallest length (smallest length of lists in this dictionary) should return

'e, 'f', 'g'

And those with the greatest value(the sum of the integers in each list) should return

'b' 'c'

I have tried

min_value = min(dict.itervalues())
min_keys = [k for k in d if dict[k] == min_value]

But that does not give me the result I want.

Any ideas?

Thanks!

user1530318
  • 25,507
  • 15
  • 37
  • 48
  • Seems like something a filtering operation could do for you. – Wug Jul 30 '12 at 22:26
  • 2
    You *just* asked this question but changed one small detail. – Blender Jul 30 '12 at 22:26
  • possible duplicate of [Python miminum value in dictionary of lists](http://stackoverflow.com/questions/11729670/python-miminum-value-in-dictionary-of-lists) – Wooble Jul 30 '12 at 22:27

1 Answers1

7
def get_smallest_length(x):
    return [k for k in x.keys() if len(x.get(k))==min([len(n) for n in x.values()])]

def get_largest_sum(x):
    return [k for k in x.keys() if sum(x.get(k))==max([sum(n) for n in x.values()])]

x = {'a': [4, 2], 'c': [4, 3], 'b': [3, 4], 'e': [4], 'd': [4, 3], 'g': [4], 'f': [4]}

print get_smallest_length(x)
print get_largest_sum(x)

Returns:

['e', 'g', 'f']
['c', 'b', 'd']
garnertb
  • 9,454
  • 36
  • 38