# Python Dictionary Questions Flashcards

1
Q

Convert these two Python lists to a dictionary

keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
A

mydict = dict(zip(keys, values))
mydict # {‘Ten’:10, ‘Twenty’: 20, ‘Thirty’:30}

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

Merge two dictionaries into one

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
A

dict3 = {**dict1, **dict2}
dict3 {‘Ten’:10, ‘Twenty’:20, ‘Thirty’:30, ‘‘Fourty’:40…}

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

Merge two dictionaries into one

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
A

dict3 = dict1.copy()
dict3.update(dict2)
dict3{‘Ten’:10, ‘Twenty’:20, ‘Thirty’:30, ‘‘Fourty’:40…}

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

Access the value of key ‘history’

sampleDict = { “class”:{ “student”:{ “name”:”Mike”, “marks”:{ “physics”:70, “history”:80 } } } }

A

NameError

sampleDict[‘history’]

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

Create a new dict by extracting the keys from the below dict

sampleDict = { “name”: “Kelly”, “age”:25, “salary”: 8000, “city”: “New york” }

A

newDict = {k: sampleDict[k] for k in keys}
newDict {‘name’: ‘Kelly’, ‘salary’: 8000}

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

Delete set of keys from a dictionary

sampleDict = { “name”: “Kelly”, “age”:25, “salary”: 8000, “city”: “New york” }

keysToRemove = [“name”, “salary”]

A

sampleDict = {k: sampleDict[k] for k in sampleDict.keys() - keysToRemove}
sampleDict {‘city’: ‘New york’, ‘age’:25}

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

Check if a value 200 exists in a dictionary

sampleDict = {‘a’: 100, ‘b’: 200, ‘c’: 300}

A

200 in sampleDict.values() # True

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

Rename key city to location

sampleDict = { “name”: “Kelly”, “age”:25, “salary”: 8000, “city”: “New york” }

A

sampleDict[‘location’] = sampleDict.pop(‘city’)
sampleDict {‘name’:’Kelly’, ‘age’:25, ‘salary’:8000, ‘location’: ‘New york’}

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

Get the key of a minimum value

sampleDict = { ‘Physics’: 82, ‘Math’: 65, ‘history’: 75 }

A

min(sampleDict, key=sampleDict.get) # Math

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

Change Brad’s salary to 8500

sampleDict = { ‘emp1’: {‘name’: ‘Jhon’, ‘salary’: 7500}, ‘emp2’: {‘name’: ‘Emma’, ‘salary’: 8000}, ‘emp3’: {‘name’: ‘Brad’, ‘salary’: 6500} }

A

sampleDict[‘emp3’][‘salary’] = 8500

{‘emp1’: {‘name’: ‘Jhon’, ‘salary’: 7500},
‘emp2’: {‘name’: ‘Emma’, ‘salary’: 8000},
‘emp3’: {‘name’: ‘Brad’, ‘salary’: 8500}}

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