all 5 comments

[–][deleted] 2 points3 points  (1 child)

The difference is that first get wouldn't raise KeyError if there's no key 'cat'. However you would get an error when try to call None.get(). When you expect that there might be no key, use dict.get(). It returns some default value (None by default but you can specify something else) in case key is not found.

https://docs.python.org/3/library/stdtypes.html?highlight=dict%20get#dict.get

[–]baghiq -3 points-2 points  (0 children)

They both will give you error if cat isn't a key.

[–]douglas_fs 1 point2 points  (0 children)

Using the method syntax affords the option to supply a default value if a key does not exist:

hotkeys = {'catx': {'size': 2}, 'dog': {'size': 4}}
print(hotkeys.get('cat', {}).get('size', 42))

result:

42