Search code examples
pythonpython-3.xpython-itertoolsitertools-groupby

returning min from dict values where there are more than one zeros


I have a weird situation with a dict which looks like so:

a={
    "key1":[{"a":10,"b":10,"d":0},{"a":100,"b":100,"d":1},{"a":1000,"b":1000,"d":0}], 
    "key2":[{"a":18,"b":135,"d":0},{"a":135,"b":154,"d":10},{"a":123,"b":145,"d":0}],
    "key3":[{"a":145,"b":1455,"d":0},{"a":15,"b":12,"d":1},{"a":14,"b":51,"d":10}]
}

I can get the min by key doing this:

[min(group, key=itemgetter("d")) for group in a.values()]
[{'a': 10, 'b': 10, 'd': 0}, 
 {'a': 18, 'b': 135, 'd': 0}, 
 {'a': 145, 'b': 1455, 'd': 0}
]

However this misses entries where where minimum value is more than one zero. So, I would like it to return:

[{'a': 10, 'b': 10, 'd': 0},
 {"a":1000,"b":1000,"d":0},
 {'a': 18, 'b': 135, 'd': 0}, {"a":123,"b":145,"d":0},
 {'a': 145, 'b': 1455, 'd': 0}
]

How can I force this condition


Solution

  • You need to first get the minimum value of d in each group, then filter the group to the elements with that value of d.

    result = []
    for group in a.values():
        dmin = min(group, key=itemgetter("d"))['d']
        result.extend(x for x in group if x['d'] == dmin)