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.

Answer from Weeble on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_dictionary_get.asp
Python Dictionary get() Method
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else · Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match ... Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Matplotlib Intro Matplotlib Get Started Matplotlib Pyplot Matplotlib Plotting Matplotlib Markers Matplotlib Line Matplotlib Labels Matplotlib Grid Matplotlib Subplot Matplotlib Scatter Matplotlib Bars Matplotlib Histograms Matplotlib Pie Charts
🌐
Reddit
reddit.com › r/learnpython › what does get() mean and do in python?
r/learnpython on Reddit: What does get() mean and do in Python?
March 17, 2025 -

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.

When to use dict.get in Python (timing) Jan 18, 2022
r/Python
4y ago
'Get' methods in Python Oct 26, 2019
r/learnpython
6y ago
Using get() with nested dictionary. Apr 10, 2024
r/learnpython
2y ago
More results from reddit.com
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.7 documentation
The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. Extracting a value for a non-existent key by subscripting (d[key]) raises a KeyError. To avoid getting this error when trying to access a possibly non-existent key, use the get() method instead, which returns None (or a specified default value) if the key is not in the dictionary.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-get-method
Python Dictionary get() Method - GeeksforGeeks
April 18, 2026 - The dict.get() method in Python returns the value associated with a given key. If the key is not present, it returns None by default or a specified default value if provided. It allows safe access to dictionary keys without raising a KeyError.
Top answer
1 of 6
12

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.

2 of 6
3

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.

🌐
Codecademy
codecademy.com › learn › dscp-python-fundamentals › modules › dscp-python-dictionaries › cheatsheet
Python Fundamentals: Python Dictionaries Cheatsheet | Codecademy
For dict1.update(dict2), the key-value pairs of dict2 will be written into the dict1 dictionary. For keys in both dict1 and dict2, the value in dict1 will be overwritten by the corresponding value in dict2. ... Python allows the values in a dictionary to be any type – string, integer, a list, another dictionary, boolean, etc.
🌐
Reddit
reddit.com › r/pythontips › beginner tip - use dictionary .get() method to improve code readability when using dictionaries
r/pythontips on Reddit: Beginner Tip - Use Dictionary .get() method to Improve Code Readability When Using Dictionaries
February 16, 2023 -

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 + 1

This 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!

Find elsewhere
🌐
Medium
medium.com › @colinforster_75524 › stop-using-keys-to-access-dictionary-values-use-the-get-method-instead-c47e087bddf7
Stop Using Keys to Access Dictionary Values. Use the Get Method instead. | by crforster | Medium
January 10, 2024 - The key parameter is the key that we want to look up in the dictionary, and the default parameter is the value that we want to return if the key is not found. The default parameter is optional, and if we omit it, the .get method will return None, which is a special value in Python that represents ...
🌐
Codecademy
codecademy.com › docs › python › dictionaries › .get()
Python | Dictionaries | .get() | Codecademy
May 11, 2025 - The .get() method is a built-in dictionary method in Python that retrieves the value for a specified key from a dictionary. This method provides a safe way to access dictionary values without raising a KeyError when the key doesn’t exist.
🌐
Career Karma
careerkarma.com › blog › python › python dictionary get: step-by-step guide
Python Dictionary Get: Step-By-Step Guide | Career Karma
December 1, 2023 - The dict.get method allows Python coders to retrieve a value associated with a specified key in a dictionary. Learn how to use the dict.get method in your code on Career Karma.
🌐
Programiz
programiz.com › python-programming › methods › dictionary › get
Python Dictionary get()
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The get() method returns the value of the specified key in the dictionary.
🌐
Cisco
ipcisco.com › home › python dictionary methods
Python Dictionary Methods with Examples | get, keys, values, items
March 5, 2026 - Now, let’s focus these python dictionary methods and learn them one by one. ... get() method is used to get the value of a given key. As you know there are key:value pair in python dictionaries.
🌐
YouTube
youtube.com › watch
Python dict.get() Method - YouTube
Full Tutorial: https://blog.finxter.com/python-dict-get-method/Email Academy: https://blog.finxter.com/email-academy/►► Do you want to thrive as a self-emplo...
Published: November 3, 2021
🌐
YouTube
youtube.com › watch
Using The Get Method to Access Dictionary Items - YouTube
I always trick my students with this.Ever tried accessing a dictionary key that didn't exist and got slammed with a KeyError? 🔑 Switch to Python's `get` met...
Published: October 13, 2024
🌐
Python Snacks
pythonsnacks.com › python snacks › using the `get` method to fetch values from a python dictionary
Using the `get` method to fetch values from a Python dictionary
April 15, 2025 - Using the `get` method to fetch values from a Python dictionary · Here's a quick tutorial as to why you should use this versus a try and except block. byBrandon Molyneaux · Dec 13, 2023 ·
🌐
Tutorialspoint
tutorialspoint.com › python › dictionary_get.htm
Python dictionary get() Method
The Python dictionary get() method is used to retrieve the value corresponding to the specified key. This method accept keys and values, where value is optional. If the key is not found in the dictionary, but the value is specified, then this method
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get a Value from a Dictionary by Key in Python | note.nkmk.me
April 23, 2025 - This article explains how to get a value from a dictionary (dict) by key in Python. Get a value from a dictionary with dict[key] (KeyError for non-existent keys) Use dict.get() to get the default valu ...
🌐
docs.python.org
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.7 documentation
The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some colle...