In this python dictionary program, we will find the shortest list of values with the keys in a given dictionary.
Steps to solve the program
- Take a dictionary in the given format as input and create an empty list.
- Use for loop to iterate over keys and values of the input dictionary.
- If the length of the value list is 1, then add the key for the value list to the empty list.
- Print the output.
D1 = {'a':[10,12],'b':[10],'c':[10,20,30,40],'d':[20]}
keys = []
for k,v in D1.items():
if len(v) == 1:
keys.append(k)
print(keys)
Output :
['b', 'd']