Professional Documents
Culture Documents
Pract9
Pract9
Output:
Subject: PWP[22616]
Practical No9.: Dictionary Operations.
Name: Ritesh Doibale Roll no:41
Practical Code:
1.What is the Output following program.
dictionary = {'MSBTE' : 'Maharashtra State Board of Technical Education',
'CO' : 'Computer Engineering',
'SEM' : 'Sixth'
}
del dictionary['CO'];
for key, values in dictionary.items():
print(key)
dictionary.clear();
for key, values in dictionary.items():
print(key)
del dictionary;
for key, values in dictionary.items():
print(key)
Output:
2. What is Output.
dictionary1 = {'Google' : 1,
'Facebook' : 2,
'Microsoft' : 3
}
dictionary2 = {'GFG' : 1,
'Microsoft' : 2,
'Youtube' : 3
}
dictionary1.update(dictionary2);
for key, values in dictionary1.items():
print(key, values)
Output:
3. What is Output.
temp = dict()
temp['key1'] = {'key1' : 44, 'key2' : 566}
temp['key2'] = [1, 2, 3, 4]
for (key, values) in temp.items():
print(values, end = "")
Subject: PWP[22616]
Practical No9.: Dictionary Operations.
Name: Ritesh Doibale Roll no:41
Output:
X.Exercise.
1. Write a Python script to sort (ascending and descending) a dictionary by value.
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_d = dict(sorted(d.items(), key=lambda item: item[1]))
print("Dictionary in ascending order by value : ", sorted_d)
sorted_d = dict(sorted(d.items(), key=lambda item: item[1], reverse=True))
print("Dictionary in descending order by value : ", sorted_d)
Output:
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}
Output:
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"}]
data = [{"V":"S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":"S005"}, {"V":"S009"},
{"VIII":"S007"}]
unique_values = set(val for dic in data for val in dic.values())
for value in unique_values:
print(value)
print(len(unique_values))
Output:
d = {'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 500, 'f': 600, 'g': 700, 'h': 800, 'i': 900}
highest_values = sorted(d.values(), reverse=True)[:3]
for i, value in enumerate(highest_values):
print(f"{value}: ", end="")
for k, v in d.items():
if v == value:
print(k, end=" ")
print()
Output: