SET A]
1. Write a Python script to access the value of a key from a dictionary.
→my_dict = {'name': 'Siddharth', 'age': 21, 'city': 'Indore'}
print("Value of 'name':", my_dict['name'])
print("Value of 'city':", my_dict['city'])
2. Write a Python script to concatenate following dictionaries to create a new
one. Sample Dictionary:
dic1={1:10,2:20}
dic2={3:30,4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
→dic1 = {1: 10, 2: 20}
dic2 = {3: 30, 4: 40}
dic3 = {5: 50, 6: 60}
result = {}
[Link](dic1) #update() is a method used with dictionaries to add items from one
dictionary into another.
[Link](dic2)
[Link](dic3)
print("Concatenated Dictionary:", result)
[Link] a Python program to iterate over dictionaries using for loops.
→ student = {'name': 'Rahul', 'age': 20, 'course': '[Link]'}
for key, value in [Link]():
print(key, ":", value)
[Link] a Python program to sum all the items in a dictionary.
Sample Dictionary: my_dict={'data1':100,'data2':-54,'data3':247}
Expected Result: 293
→my_dict = {'data1': 100, 'data2': -54, 'data3': 247}
total = sum(my_dict.values())
print("Sum of values:", total)
[Link] a Python program to remove a key from a dictionary. Sample Dictionary:
myDict={'a':1,'b':2,'c':3,'d':4} Sample Output:
{'c': 3, 'b': 2, 'd': 4, 'a': 1}
{'c': 3, 'b': 2, 'd': 4}
myDict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
print("Original:", myDict)'
[Link]('a')
print("After removing 'a':", myDict)
SET B]
1. Write a Python program to sort a dictionary by key. Sample Dictionary:
color_dict = {'red':'#FF0000','green':'#008000','black':'#000000','white':'#FFFFFF'}
Expected Output:
black: #000000 36
green: #008000 red: #FF0000
white: #FFFFFF
→ color_dict = {'red':'#FF0000','green':'#008000','black':'#000000','white':'#FFFFFF'}
for key in sorted(color_dict):
print(key, ":", color_dict[key])
[Link] a Python program to combine two dictionary adding values for common
keys. Sample Dictionary:
d1={'a':100,'b':200,'c':300}
d2={'a':300,'b':200,'d':400}
Sample output: Counter({'a': 400, 'b': 400, 'd': 400, 'c': 300})
→ d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}
result = [Link]()
for key in d2:
if key in result:
result[key] = result[key] + d2[key]
else:
result[key] = d2[key]
print(result)
[Link] a Python script to generate and print a dictionary that contains a number
(Between 1 and n) in the form (x, x*x).
Sample Dictionary (n = 5)
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
→n=5
squares = {}
for x in range(1, n+1):
squares[x] = x*x
print(squares)
[Link] a Python program to create a dictionary from a string. Sample-
String:’W3resource’
Expected output: {'3': 1, 's': 1, 'r': 2, 'u': 1, 'w': 1, 'c': 1, 'e': 2, 'o': 1}
string = "W3resource"
dict_from_str = {}
for char in [Link]():
dict_from_str[char] = dict_from_str.get(char, 0) + 1
print(dict_from_str)
SET C]
1. Write a Python program to create a dictionary from two lists without losing
duplicate values.
Sample lists: ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII'], [1, 2, 2, 3] Expected Output:
defaultdict(<class 'set'>, {'Class-VII': {2}, 'Class-VI': {2}, 'Class-VIII':
{3}, 'Class-V': {1}})
→ from collections import defaultdict
keys = ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII']
values = [1, 2, 2, 3]
result = defaultdict(set)
for k, v in zip(keys, values):
result[k].add(v)
print(result)
[Link] a Python program to create a dictionary of keys x, y, and z where each key
has as value a list from 11-20, 21-30, and 31-40 respectively. Access the fifth value of
each key from the dictionary.
Expected Output:
{'x': [11, 12, 13, 14, 15, 16, 17, 18, 19],
'y': [21, 22, 23, 24, 25, 26, 27, 28, 29],
'z': [31, 32, 33, 34, 35, 36, 37, 38, 39]}
15
25
35
x has value [11, 12, 13, 14, 15, 16, 17, 18, 19]
y has value [21, 22, 23, 24, 25, 26, 27, 28, 29]
z has value [31, 32, 33, 34, 35, 36, 37, 38, 39]
→ dict_xyz = {
'x': list(range(11, 20)),
'y': list(range(21, 30)),
'z': list(range(31, 40))
print(dict_xyz)
print(dict_xyz['x'][4])
print(dict_xyz['y'][4])
print(dict_xyz['z'][4])
print("x has value", dict_xyz['x'])
print("y has value", dict_xyz['y'])
print("z has value", dict_xyz['z'])