Given a dictionary with list value elements, how can I get the key whose list value has the minimum length, on the condition that len() > 0
?
mm = {1: [2, 5], 2: [3, 4, 7], 3: [1, 4], 4: [], 5: [1], 6: [3, 4]} item = min(mm, key=lambda f: len(mm[f]))
Expected output: 5
This is one approach by checking if list is empty
Ex:
import sys mm = {1: [2, 5], 2: [3, 4, 7], 3: [1, 4], 4: [], 5: [1], 6: [3, 4]} item = min(mm, key=lambda f: len(mm[f]) if mm[f] else sys.maxsize) print(item) #5
Recent Comments