The Noobs of Python: Ep.3.0 - Methods to the Madness

Here's another handy method for dictionaries, .get()
I used it in episode 4 and thought I'd come back to cover it.

.get() is a method that can be used on dictionaries to fetch the definition of a given key. It takes one argument - the key.

It can also, optionally, take another argument, which is the value to return if the given key isn't found in the dictionary the method's being used on - the default for this is None. Be warned! Some guides will state this argument as being a keyword argument (you have to do "default = " instead of just placing the value you want to be the default between the parenthesis. This is no longer the case!

See the quoted post for a usage example.

Edit: If you want to see how to write functions with optional arguments like the .get() method's optional default value, see this post in episode 4! Yep, go back again! lol:

2 Likes