w3resource

Python: Find shortest list of values with the keys of a dictionary

Python dictionary: Exercise-60 with Solution

Write a Python program to find shortest list of values with the keys in a given dictionary.

Sample Solution:

Python Code:

def test(dictt):
    min_value=1
    result = [k for k, v in dictt.items() if len(v) == (min_value)] 
    return result    

dictt = {
 'V': [10, 12],
 'VI': [10],
 'VII': [10, 20, 30, 40],
 'VIII': [20],
 'IX': [10,30,50,70],
 'X': [80]
 }

print("\nOriginal Dictionary:")
print(dictt)
print("\nShortest list of values with the keys of the said dictionary:")
print(test(dictt))

Sample Output:

Original Dictionary:
{'V': [10, 12], 'VI': [10], 'VII': [10, 20, 30, 40], 'VIII': [20], 'IX': [10, 30, 50, 70], 'X': [80]}

Shortest list of values with the keys of the said dictionary:
['VI', 'VIII', 'X']

Pictorial Presentation:

Python Dictionary: Find shortest list of values with the keys of a dictionary.

Flowchart:

Flowchart: Find shortest list of values with the keys of a dictionary.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the specified number of maximum values in a given dictionary.
Next: Write a Python program to count the frequency in a given dictionary.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()