Dictionaries Flashcards

1
Q

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}.

A

d = {0:10, 1:20}
print(d)
d.update({2:30})
print(d)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Concatenate the following dictionaries: dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a Python script to check whether a given key already exists in a dictionary.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

d = {‘x’: 10, ‘y’: 20, ‘z’: 30}
for dict_key, dict_value in d.items():
print(dict_key,’->’,dict_value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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}

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Print a dictionary where the keys are numbers between 1 and 15 (including both numbers) and the values are the square of the keys.

A

my_dict = dict()
for x in range(1,16):
my_dict[x]=x**2
print(my_dict)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Add together all the items in a dictionary. Sample dictionary: {‘data1’:100,’data2’:-54,’data3’:247}.

A

my_dict = {‘data1’:100,’data2’:-54,’data3’:247}
add = sum(my_dict.values())
print(add)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a Python program to multiply all the items in a dictionary. Do this for the dictionary {‘data1’:100,’data2’:-54,’data3’:247}.

A

my_dict = {‘data1’:100,’data2’:-54,’data3’:247}
result = 1
for key, value in my_dict.items():
result *= value
print(result)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Remove a key from a dictionary. Remove data 1 from this dictionary {‘data1’:100,’data2’:-54,’data3’:247}.

A

my_dict = {‘data1’:100,’data2’:-54,’data3’:247}
if ‘data1’ in my_dict:
del my_dict[‘data1’]
print(my_dict)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a Python program to map two lists into a dictionary. keys = [‘red’, ‘green’, ‘blue’]
values = [‘#FF0000’,’#008000’, ‘#0000FF’]

A

keys = [‘red’, ‘green’, ‘blue’]
values = [‘#FF0000’,’#008000’, ‘#0000FF’]
color_dictionary = dict(zip(keys, values))
print(color_dictionary)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a Python program to sort a given dictionary by key. {‘red’:’#FF0000’,
‘green’:’#008000’,
‘black’:’#000000’,
‘white’:’#FFFFFF’}

A

color_dict = {‘red’:’#FF0000’,
‘green’:’#008000’,
‘black’:’#000000’,
‘white’:’#FFFFFF’}

for key in sorted(color_dict):
print(f”{key}: {color_dict[key]}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a Python program to get the maximum and minimum values of a dictionary. my_dict = {‘x’:500, ‘y’:5874, ‘z’: 560}

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly