Practical 09
Practical 09
Sr. Remark if
Name of Resource Broad Specification Quantity
No. any
1 Computer System Intel i3, 4 GB RAM
For All
2 Operating System Windows/Linux 20
Experiments
3 Development Software Python IDE And VS Code
Explanation:
1. The dictionary initially contains three key-value pairs.
2. The del dictionary['CO'] statement removes the 'CO' key.
3. The first for loop prints the remaining keys: 'MSBTE' and 'SEM'.
4. The dictionary.clear() statement removes all items, making it an empty dictionary.
5. The second for loop does not print anything because the dictionary is empty.
6. The del dictionary statement deletes the dictionary entirely.
7. The third for loop attempts to access dictionary, but since it has been deleted, Python
raises a NameError.
2. What is the output of the following program?
dictionary1 = {'Google': 1,
'Facebook': 2,
'Microsoft': 3}
dictionary2 = {'GFG': 1,
'Microsoft': 2,
'Youtube': 3}
dictionary1.update(dictionary2) # Merges dictionary2 into dictionary1
Exercise
1. Write a Python script to sort (ascending and descending) a dictionary by value.
my_dict = {'apple': 3, 'banana': 1, 'cherry': 5, 'date': 2}
# Concatenating dictionaries
new_dict = {}
new_dict.update(dic1)
new_dict.update(dic2)
new_dict.update(dic3)
3. Write a Python program to combine two dictionary adding values for common keys. a.
d1 = {'a': 100, 'b': 200, 'c':300} b. d2 = {'a': 300, 'b': 200, 'd':400}
4. Write a Python program to print all unique values in a dictionary. a. Sample Data:
[{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"},
{"V":"S009"}, {"VIII":"S007"}]
# Sample Data
data = [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"},
{"VII": "S005"}, {"V": "S009"}, {"VIII": "S007"}]
# Sorting the dictionary by values in descending order and getting the top 3 items
highest_3 = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)[:3]