The Wayback Machine - https://web.archive.org/web/20240703144831/https://www.geeksforgeeks.org/python-dictionary-get-method/
Open In App

Python Dictionary get() Method

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python Dictionary get() Method return the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).

Python Dictionary get() Method Syntax:

Syntax : Dict.get(key, default=None)

Parameters: 

  • key: The key name of the item you want to return the value from
  • Value: (Optional) Value to be returned if the key is not found. The default value is None.

Returns: Returns the value of the item with the specified key or the default value.

Python Dictionary get() Method Example:

Python3




d = {'coding': 'good', 'thinking': 'better'}
print(d.get('coding'))


Output:

good

Example 1: Python get() Method with default parameter.

Python




d = {1: '001', 2: '010', 3: '011'}
# since 4 is not in keys, it'll print "Not found"
print(d.get(4, "Not found"))


Output: 

Not found

Example 2: Python Dictionary get() method chained

The get() to check and assign in absence of value to achieve this particular task. Just returns an empty Python dict() if any key is not present.

Python3




test_dict = {'Gfg' : {'is' : 'best'}}
   
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
   
# using nested get()
# Safe access nested dictionary key
res = test_dict.get('Gfg', {}).get('is')
   
# printing result
print("The nested safely accessed value is :  " + str(res))


Output:

The original dictionary is : {'Gfg': {'is': 'best'}}
The nested safely accessed value is :  best

Time complexity: O(1) because it uses the get() method of dictionaries which has a constant time complexity for average and worst cases.
Auxiliary space: O(1) because it uses a constant amount of additional memory to store the dictionary and the string values. 



Previous Article
Next Article

Similar Reads

Use get() method to create a dictionary in Python from a list of elements
Prerequisites: Get() method for dictionaries in Python The naive approach for creating a dictionary from a list of items we can use the for loop. See the below example for better understanding. Example: li =['a', 'b', 'c', 'a', 'd', 'e', 'b', 'a'] di ={} for ele in li: # Increase the value of key # if exists if ele in di: di[ele]= di[ele]+1 else: #
2 min read
Combine similar characters in Python using Dictionary Get() Method
Let us see how to combine similar characters in a list. Example : Input : ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'] Output : ['gg', 'eeee', 'kk', 'ss', 'f', 'o', 'r'] We will be using the get() method of the dictionary class. dictionary.get() The get() method returns the value of the item with the specified key. Syntax : dic
3 min read
Python | Pretty Print a dictionary with dictionary value
This article provides a quick way to pretty How to Print Dictionary in Python that has a dictionary as values. This is required many times nowadays with the advent of NoSQL databases. Let's code a way to perform this particular task in Python. Example Input:{'gfg': {'remark': 'good', 'rate': 5}, 'cs': {'rate': 3}} Output: gfg: remark: good rate: 5
7 min read
Python | Get key from value in Dictionary
Let's see how to get the key by value in Python Dictionary. Example: One-Liner code C/C++ Code # creating a new dictionary my_dict ={"Java":100, "Python":112, "C":11} # one-liner print("One line Code Key value: ", list(my_dict.keys()) [list(my_dict.values()).index(100)]) Output: Java Extract Key from Python D
6 min read
Get length of dictionary in Python
To calculate the length of a dictionary, we can use the Python built-in len() method. The len() method returns the number of keys in a Python dictionary. Python Dict len() Syntax Syntax: len(Dict) Return: It returns an integer which is the length of the string. Name:Steve Age:30 Designation:ProgrammerBasic example of finding the length of a diction
5 min read
Get a dictionary from an Objects Fields
In this article, we will discuss how to get a dictionary from object's field i.e. how to get the class members in the form of a dictionary. There are two approaches to solve the above problem: By using the __dict__ attribute on an object of a class and attaining the dictionary. All objects in Python have an attribute __dict__, which is a dictionary
2 min read
Python Dictionary fromkeys() Method
Python dictionary fromkeys() function returns the dictionary with key mapped and specific value. It creates a new dictionary from the given sequence with the specific value. Python Dictionary fromkeys() Method Syntax: Syntax : fromkeys(seq, val) Parameters : seq : The sequence to be transformed into a dictionary.val : Initial values that need to be
3 min read
Python Dictionary setdefault() Method
Python Dictionary setdefault() returns the value of a key (if the key is in dictionary). Else, it inserts a key with the default value to the dictionary. Python Dictionary setdefault() Method Syntax: Syntax: dict.setdefault(key, default_value)Parameters: It takes two parameters: key - Key to be searched in the dictionary. default_value (optional) -
2 min read
Python Dictionary update() method
Python Dictionary update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs. Example: Original dictionary : {'A': 'Geeks', 'B': 'For'}Updated dictionary : {'A': 'Geeks', 'B': 'Geeks'}Original dictionary : {'A': 'Geeks', 'B': 'For'}Updated dictionary : {'A': 'Geeks', 'B': 'For', 'C
3 min read
Python Dictionary items() method
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.In Python Dictionary, items() method is used to return the list with all dictionary keys with values. Syntax: dictionary.items()Parameters: T
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg