Question 35: Write a Python script to sort (ascending and descending) a dictionary by value. Download hole Program / Project code, by clicking following link: What is the output of the following program? ? # Define a dictionary dictionary = {'MSBTE': 'Maharashtra State Board of Technical Education',
'CO': 'Computer Engineering',
'SEM': 'Sixth'}
# Delete the key 'CO' from the dictionary
del dictionary['CO']
# Print the remaining keys in the dictionary
for key, value in dictionary.items():
print(key)
# Clear the dictionary (remove all items)
dictionary.clear()
# Try to print the keys again (this will not print anything)
for key, value in dictionary.items():
print(key)
# Delete the entire dictionary
del dictionary
# Attempt to print keys from a deleted dictionary (this will not execute)
for key, value in dictionary.items():
print(key)
The provided Python program manipulates a dictionary and tries to print its keys. Let's break down the program step by step and predict the output: - del dictionary['CO'] removes the key 'CO' and its associated value from the dictionary.
- The program then enters a loop to print the keys using for key, values in dictionary.items():. However, since the dictionary no longer contains the key 'CO', only the remaining keys will be printed.
- dictionary.clear() clears all the contents of the dictionary.
- The program then tries to print the keys again using the same loop. Since the dictionary is now empty, no keys will be printed.
- Finally, del dictionary deletes the entire dictionary.
Here's the expected output of the program:
MSBTE
SEM
Explanation: - In the first loop, only the keys 'MSBTE' and 'SEM' are printed because the key 'CO' was deleted.
- In the second loop, nothing is printed because the dictionary is empty after calling dictionary.clear().
- In the third loop, nothing is printed because the dictionary itself has been deleted with del dictionary. Therefore, this loop will not execute, and no error will be raised since the dictionary no longer exists.
Programming Code: Following code write in: BP_P35.py # Sort ascending & descending a dictionary by value
# import lib
import operator
# create dictionary
dict1 = {1:2, 3:4, 4:3, 2:1, 0:0}
print("dictionary is:", dict1)
# sort dictionary
sorted_dict1 = sorted(dict1.items(), key=operator.itemgetter(1))
print("Ascending order is: ", sorted_dict1)
desc_sorted_dict1 = dict(sorted(dict1.items(), key=operator.itemgetter(1), reverse=True))
print("Descending order is: ", desc_sorted_dict1)
# Thanks for Reading.
Output:
What is the output of the following program? ?
# Define a dictionary
dictionary = {'MSBTE': 'Maharashtra State Board of Technical Education',
'CO': 'Computer Engineering',
'SEM': 'Sixth'}
# Delete the key 'CO' from the dictionary
del dictionary['CO']
# Print the remaining keys in the dictionary
for key, value in dictionary.items():
print(key)
# Clear the dictionary (remove all items)
dictionary.clear()
# Try to print the keys again (this will not print anything)
for key, value in dictionary.items():
print(key)
# Delete the entire dictionary
del dictionary
# Attempt to print keys from a deleted dictionary (this will not execute)
for key, value in dictionary.items():
print(key)
'CO': 'Computer Engineering',
'SEM': 'Sixth'}
# Delete the key 'CO' from the dictionary
del dictionary['CO']
# Print the remaining keys in the dictionary
for key, value in dictionary.items():
print(key)
# Clear the dictionary (remove all items)
dictionary.clear()
# Try to print the keys again (this will not print anything)
for key, value in dictionary.items():
print(key)
# Delete the entire dictionary
del dictionary
# Attempt to print keys from a deleted dictionary (this will not execute)
for key, value in dictionary.items():
print(key)
The provided Python program manipulates a dictionary and tries to print its keys. Let's break down the program step by step and predict the output:
- del dictionary['CO'] removes the key 'CO' and its associated value from the dictionary.
- The program then enters a loop to print the keys using for key, values in dictionary.items():. However, since the dictionary no longer contains the key 'CO', only the remaining keys will be printed.
- dictionary.clear() clears all the contents of the dictionary.
- The program then tries to print the keys again using the same loop. Since the dictionary is now empty, no keys will be printed.
- Finally, del dictionary deletes the entire dictionary.
Here's the expected output of the program:
MSBTE
SEM
MSBTE
SEM
Explanation:
- In the first loop, only the keys 'MSBTE' and 'SEM' are printed because the key 'CO' was deleted.
- In the second loop, nothing is printed because the dictionary is empty after calling dictionary.clear().
- In the third loop, nothing is printed because the dictionary itself has been deleted with del dictionary. Therefore, this loop will not execute, and no error will be raised since the dictionary no longer exists.
# Sort ascending & descending a dictionary by value # import lib import operator # create dictionary dict1 = {1:2, 3:4, 4:3, 2:1, 0:0} print("dictionary is:", dict1) # sort dictionary sorted_dict1 = sorted(dict1.items(), key=operator.itemgetter(1)) print("Ascending order is: ", sorted_dict1) desc_sorted_dict1 = dict(sorted(dict1.items(), key=operator.itemgetter(1), reverse=True)) print("Descending order is: ", desc_sorted_dict1) # Thanks for Reading.
Output:
0 Comments