You are on page 1of 2

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 2.3

Student Name: SAGAR ROHILLA UID: 21BCS1383`


Branch: CSE Section/Group: 618 -A
Semester: 4th semester Date of Performance:24/04/23
Subject Name: Programming in Python Lab Subject Code: 21CSP-259

1. Aim:
Program to demonstrate creation, accessing of dictionary and apply different kinds of
operations on them.
 Write a Python program to combine two dictionary adding values for common
keys. d1 = {'a': 100, 'b': 200, 'c':300}, d2 = {'a': 300, 'b': 200, 'd':400}
 Write a Python program to find the highest 3 values of corresponding keys in a
dictionary.

2. Source Code:
#Write a Python program to combine two dictionary adding values for common keys.
d1 = {'a': 100, 'b': 200, 'c':300}, d2 = {'a': 300, 'b': 200, 'd':400}

print("SAGAR ROHILLA 21BCS1383")


from collections import Counter
dict1 = {'a': 100, 'b': 200, 'c':300}
dict2 = {'a': 300, 'b': 200, 'd':400}
new_dict = Counter(dict1) + Counter(dict2)
print("The new dict is:", new_dict)

#Write a Python program to find the highest 3 values of corresponding keys in a dictionary.
print("SAGAR ROHILLA 21BCS1383")
def highest_3_values_1(my_dict):
result_dict = {}
sorted_dict = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
for i in range(3):
result_dict[sorted_dict[i][0]] = sorted_dict[i][1]
return result_dict
my_dict = {'A': 67, 'B': 23, 'C': 45, 'D': 56, 'E': 12, 'F': 69}
print(highest_3_values_1(my_dict))
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Screenshot of outputs:
OUTPUT_1

OUTPUT_2

You might also like