CBSE Class 12 Computer Science Question 50 of 57

Review of Python Basics — Question 50

Back to all questions
50
Question

Question 45

Write a Python program to sort a list alphabetically in a dictionary.

Solution
my_dict = eval(input("Enter the dictionary: "))
for key, value in my_dict.items():
    value.sort()

print("Sorted dictionary:", my_dict)
Output
Enter the dictionary: {'list1': ['banana', 'apple', 'cherry'], 'list2': ['orange', 'grape', 'kiwi'], 'list3': ['pear', 'watermelon', 'mango']}
Sorted dictionary: {'list1': ['apple', 'banana', 'cherry'], 'list2': ['grape', 'kiwi', 'orange'], 'list3': ['mango', 'pear', 'watermelon']}
Answer

my_dict
=
eval
(
input
(
"Enter the dictionary: "
))
for
key
,
value
in
my_dict
.
items
():
value
.
sort
()
print
(
"Sorted dictionary:"
,
my_dict
)
Output
Enter the dictionary: {'list1': ['banana', 'apple', 'cherry'], 'list2': ['orange', 'grape', 'kiwi'], 'list3': ['pear', 'watermelon', 'mango']}
Sorted dictionary: {'list1': ['apple', 'banana', 'cherry'], 'list2': ['grape', 'kiwi', 'orange'], 'list3': ['mango', 'pear', 'watermelon']}