Python Practice › Dictionary Programs › Program 40
Sort the list values
Write a Python program to sort each list value of a dictionary.
Input
{"a1": [1, 5, 3], "a2": [10, 6, 20]}Output
{'a1': [1, 3, 5], 'a2': [6, 10, 20]}Approach
{k: sorted(v) ...}.
Solution
d = {"a1": [1, 5, 3], "a2": [10, 6, 20]}print({k: sorted(v) for k, v in d.items()})Output
{'a1': [1, 3, 5], 'a2': [6, 10, 20]}Learn the concept
New to this topic? Read the lesson, then come back.