Videos
Many times in coding interviews we work with simple dictionaries with structure as follows:
my_dict = {"key1": 10, "key2": 20, "key3": 30}In many scenarios, we want to check if a key exists in a dictionary, and if so, do something with that key, and reassign it. Example...
key = 'something'
if key in my_dict:
print('Already Exists')
value = my_dict[key]
else:
print('Adding key')
value = 0
my_dict[key] = value + 1This is a common workflow seen in many leet code style questions and in practice.
However it is not ideal and is a little noisy, we can do exactly this with the .get() method for python dictionaries
value = my_dict.get(key, 0) my_dict[key] = value + 1
It does the same thing as above with fewer lines of code and fewer accesses to the dictionary itself!
So I recommend beginners be aware of this.
I have a Youtube video on how to use it as well, with more details :) https://www.youtube.com/watch?v=uNcvhS5OepM
If you are a Python beginner and enjoy learning simple ways to help you improve your Python abilities please like the video and subscribe to my channel! Would appreciate it, and I think you can learn some useful skills along the way!
Hi, I am taking a more advance Python course at university and I have a difficult time understanding what the get() method does in Python. I know it is something to do with dictionaries but even after doing some research online, I still couldn't quite understand it fully. So can anyone please explain to me what the get() method does in Python in a simple definition, it would be helpful, thanks.
The get method on a dictionary is documented here: https://docs.python.org/3/library/stdtypes.html#dict.get
get(key[, default])
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
So this explains the 0 - it's a default value to use when letternum doesn't contain the given letter.
So we have letternum.get(each_letter, 0) - this expression finds the value stored in the letternum dictionary for the currently considered letter. If there is no value stored, it evaluates to 0 instead.
Then we add one to this number: letternum.get(each_letter, 0) + 1
Finally we stored it back into the letternum dictionary, although this time converting the letter to lowercase: letternum[each_letter.lower()] = letternum.get(each_letter, 0) + 1 It seems this might be a mistake. We probably want to update the same item we just looked up, but if each_letter is upper-case that's not true.
letternum is a dict (a dictionary). It has a method called get which returns the value associated with a given key. If the key is absent from the dictionary, it returns a default value, which is None unless an optional second argument is present, in which case that argument value is returned for missing elements.
In this case, letternum.get(each_letter,0) returns letternum[each_letter] if each_letter is in the dictionary. Otherwise it returns 0. Then the code adds 1 to this value and stores the result in letternum[each_letter.lower()].
This creates a count of the number of occurrences of each letter, except that it inconsistently converts the letter to lowercase when updating, but not when retrieving existing values, so it won't work properly for uppercase letters.