Sort the list values

EasyDictionary Programs · 40 of 94

Write a Python program to sort each list value of a dictionary.

Advertisement
Input
{"a1": [1, 5, 3], "a2": [10, 6, 20]}
Output
{'a1': [1, 3, 5], 'a2': [6, 10, 20]}

Approach

  1. {k: sorted(v) ...}.

Solution

Python
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]}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs