# Python Dictionary Questions Flashcards
Convert these two Python lists to a dictionary
keys = ['Ten', 'Twenty', 'Thirty'] values = [10, 20, 30]
mydict = dict(zip(keys, values))
mydict # {‘Ten’:10, ‘Twenty’: 20, ‘Thirty’:30}
Merge two dictionaries into one
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30} dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
dict3 = {**dict1, **dict2}
dict3 {‘Ten’:10, ‘Twenty’:20, ‘Thirty’:30, ‘‘Fourty’:40…}
Merge two dictionaries into one
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30} dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
dict3 = dict1.copy()
dict3.update(dict2)
dict3{‘Ten’:10, ‘Twenty’:20, ‘Thirty’:30, ‘‘Fourty’:40…}
Access the value of key ‘history’
sampleDict = { “class”:{ “student”:{ “name”:”Mike”, “marks”:{ “physics”:70, “history”:80 } } } }
NameError
sampleDict[‘history’]
Create a new dict by extracting the keys from the below dict
sampleDict = { “name”: “Kelly”, “age”:25, “salary”: 8000, “city”: “New york” }
newDict = {k: sampleDict[k] for k in keys}
newDict {‘name’: ‘Kelly’, ‘salary’: 8000}
Delete set of keys from a dictionary
sampleDict = { “name”: “Kelly”, “age”:25, “salary”: 8000, “city”: “New york” }
keysToRemove = [“name”, “salary”]
sampleDict = {k: sampleDict[k] for k in sampleDict.keys() - keysToRemove}
sampleDict {‘city’: ‘New york’, ‘age’:25}
Check if a value 200 exists in a dictionary
sampleDict = {‘a’: 100, ‘b’: 200, ‘c’: 300}
200 in sampleDict.values() # True
Rename key city to location
sampleDict = { “name”: “Kelly”, “age”:25, “salary”: 8000, “city”: “New york” }
sampleDict[‘location’] = sampleDict.pop(‘city’)
sampleDict {‘name’:’Kelly’, ‘age’:25, ‘salary’:8000, ‘location’: ‘New york’}
Get the key of a minimum value
sampleDict = { ‘Physics’: 82, ‘Math’: 65, ‘history’: 75 }
min(sampleDict, key=sampleDict.get) # Math
Change Brad’s salary to 8500
sampleDict = { ‘emp1’: {‘name’: ‘Jhon’, ‘salary’: 7500}, ‘emp2’: {‘name’: ‘Emma’, ‘salary’: 8000}, ‘emp3’: {‘name’: ‘Brad’, ‘salary’: 6500} }
sampleDict[‘emp3’][‘salary’] = 8500
{‘emp1’: {‘name’: ‘Jhon’, ‘salary’: 7500},
‘emp2’: {‘name’: ‘Emma’, ‘salary’: 8000},
‘emp3’: {‘name’: ‘Brad’, ‘salary’: 8500}}