15 Prog
15 Prog
Expected output
Result: True
Exercise 2: Write a program to iterate over dictionary items using for loop.
Expected output
Value of key 0 is Value 1
Value of key 1 is Value 2
Expected output
Result: dict_keys([0, 1, 2])
Expected output
dict_values([‘Value 1’, ‘Value 2’, ‘Value 3’])
Given keys = [1,2,3] values = [‘Value 1’, ‘Value 2’, ‘Value 3’]
Expected output
{1: ‘Value 1’, 2: ‘Value 2’, 3: ‘Value 3’}
Note: If we remove the key of the dictionary then the respective values of the dictionary
should also be deleted. This means there are no values that exist without keys.
Given:
{0:”Value 1″, 1:”Value 2″, 2:”Value 3″}
keys_to_remove = [0,1]
Expected output
Result : {2: ‘Value 3’}
Given
d = {‘key 1’: 2, ‘key 2’: 3, ‘key 3’: 4}
Expected output
Ascending: [(‘key 1’, 2), (‘key 2’, 3), (‘key 3’, 4)] Descending:
[(‘key 3’, 4), (‘key 2’, 3), (‘key 1’, 2)]
Given
dict1 = {‘key 1’: 2, ‘key 2’: 3}
dict2 = {‘key 3’: 4, ‘key 4’: 5}
Expected output
{‘key 1’: 2, ‘key 2’: 3, ‘key 3’: 4, ‘key 4’: 5}
Expected output
Result: 500
Exercise 10: Write a program to get the maximum and minimum value of dictionary.
Expected output
Max: 300
Min: 200
# printing result
print(f"{max} is maximum")
print(f"{min} is mimimum")
Exercise 11: Write a program to check if a dictionary is empty or not.
Given dict1 = {}
Expected output
Is dictionary empty ? : True
Exercise 12: Write a program in Python to choose a random item from a list.
dict1 = {
‘key 1’: ‘Apple’, ‘key 2 ′:’Mango’,
‘key 3′:’Papaya’
}
Expected output
key 1: Apple
key 2: Mango
key 3: Papaya
# Sorting dictionary by value
dict1 = {
'key 1': 'Apple',
'key 2':'Mango',
'key 3':'Papaya'
}
for key in sorted(dict1):
# printing result
print("%s : %s" % (key, dict1[key]))
Exercise 14: Write a program to check whether a key exists in the dictionary or not.
dict1 = {‘key 1’: 22, ‘key 2’: 301}
Expected output
Key exists in dictionary
Exercise 16: Write a program in Python to remove repetitive items from a list.
num = [2,3,4,5,2,6,3,2]
x = []
for i in range(len(num)):
if num[i] not in x:
x.append(num[i])
else:
pass
# printing result
print(x)