0

Here is my dictionary:

D = {'G': ['R'],
 'L': ['H'],
 'H': ['H', 'L', 'T', 'M'],
 'T': ['R'],
 'S': ['M', 'L', 'H'],
 'M': ['R']} 

I am looking to create a list of the keys with the shortest list of values in this dictionary. In this case, I would get the output:

['G', 'L', 'T', 'M']

I have this code which successfully finds the first key with the shortest list of values, however, does not create a list.

min_key, min_value = min(d.items(), key = lambda x: len(set(x[1])))

This code simply returns 'G'.

This was my attempt at creating a list:

li = [k for k, v in d.items() if v == min_key] 
li

This returns an empty list.

  • Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Fredrik Apr 06 '20 at 07:23

1 Answers1

0

Since you have the shortest list stored in min_value, you could use the length of that, and do

>>> [k for k, v in d.items() if len(v) == len(min_value)] 
['G', 'L', 'T', 'M']
Badgy
  • 819
  • 4
  • 14