Dictionary Access with Get
Stop Using Square Brackets To Get Dictionary Items — Use .get()
Before
nik = {
'age':32,
'gender':'male',
'employed':True,
}
print(nik['location'])
# Returns:
# KeyError: 'location'
After
nik = {
'age':32,
'gender':'male',
'employed':True,
}
print(nik.get('location'))
# Returns:
# None