Dictionaries Flashcards
How do you add a key/value to a dictionary? E.g. add key value pair {2:30} to the original dictionary {0:10, 1:20}.
d = {0:10, 1:20}
print(d)
d.update({2:30})
print(d)
Concatenate the following dictionaries: dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
for d in (dic1, dic2, dic3):
dic4.update(d)
print(dic4)
Write a Python script to check whether a given key already exists in a dictionary.
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
def is_key_present(x):
if x in d:
print(‘Key is present in the dictionary’)
else:
print(‘Key is not present in the dictionary’)
is_key_present(5)
is_key_present(9)
Write a Python program to iterate over dictionaries using for loops. Sample dictionary is {‘x’: 10, ‘y’: 20, ‘z’: 30} and you want to output a message saying what the dict_key is and the dict_value.
d = {‘x’: 10, ‘y’: 20, ‘z’: 30}
for dict_key, dict_value in d.items():
print(dict_key,’->’,dict_value)
Write a Python script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x). Go to the editor
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
n=int(input(“Input a number “))
d = dict()
for x in range(1,n+1):
d[x]=x*x # [ ]assigns x as the key in the dictionary
print(d)
Print a dictionary where the keys are numbers between 1 and 15 (including both numbers) and the values are the square of the keys.
my_dict = dict()
for x in range(1,16):
my_dict[x]=x**2
print(my_dict)
Add together all the items in a dictionary. Sample dictionary: {‘data1’:100,’data2’:-54,’data3’:247}.
my_dict = {‘data1’:100,’data2’:-54,’data3’:247}
add = sum(my_dict.values())
print(add)
Write a Python program to multiply all the items in a dictionary. Do this for the dictionary {‘data1’:100,’data2’:-54,’data3’:247}.
my_dict = {‘data1’:100,’data2’:-54,’data3’:247}
result = 1
for key, value in my_dict.items():
result *= value
print(result)
Remove a key from a dictionary. Remove data 1 from this dictionary {‘data1’:100,’data2’:-54,’data3’:247}.
my_dict = {‘data1’:100,’data2’:-54,’data3’:247}
if ‘data1’ in my_dict:
del my_dict[‘data1’]
print(my_dict)
Write a Python program to map two lists into a dictionary. keys = [‘red’, ‘green’, ‘blue’]
values = [‘#FF0000’,’#008000’, ‘#0000FF’]
keys = [‘red’, ‘green’, ‘blue’]
values = [‘#FF0000’,’#008000’, ‘#0000FF’]
color_dictionary = dict(zip(keys, values))
print(color_dictionary)
Write a Python program to sort a given dictionary by key. {‘red’:’#FF0000’,
‘green’:’#008000’,
‘black’:’#000000’,
‘white’:’#FFFFFF’}
color_dict = {‘red’:’#FF0000’,
‘green’:’#008000’,
‘black’:’#000000’,
‘white’:’#FFFFFF’}
for key in sorted(color_dict):
print(f”{key}: {color_dict[key]}”)
Write a Python program to get the maximum and minimum values of a dictionary. my_dict = {‘x’:500, ‘y’:5874, ‘z’: 560}
using lambda
my_dict = {‘x’:500, ‘y’:5874, ‘z’: 560}
key_max = max(my_dict.keys(), key=(lambda k: my_dict[k]))
key_min = min(my_dict.keys(), key=(lambda k: my_dict[k]))
print(‘Maximum Value: ‘,my_dict[key_max])
print(‘Minimum Value: ‘,my_dict[key_min])
my_dict = {‘x’:500, ‘y’:5874, ‘z’: 560}
max_value = max(my_dict.values())
print(max_value)
min_value = min(my_dict.values())
print(min_value)