Say that I have
function(Dict[str, List[str]] -> List[str]
how could I return a list that contains the str in the Dict with most elements in the list?
function({'a':['1','2'], 'b':['1'], 'c':['1', '2']})
['a', 'c']
The function should be able to still return the str even if more than one str has the most elements in the list just like the above example. Thank you very much in advance!
>Solution :
Get the max length first, then loop over the elements to filter those that have the max length:
def get_max(d):
max_len = max(map(len,d.values()))
return [k for k,v in d.items() if len(v)==max_len]
get_max({'a':['1','2'], 'b':['1'], 'c':['1', '2']})
# ['a', 'c']