Python Dictionary Methods
Python Dictionary is like a map that is used to store data in the form of a key:value pair. Python provides various in-built functions to deal with dictionaries. In this article, we will see a list of all the functions provided by the Python to work with dictionaries.
Accessing Dictionary
Below functions can be used to access dictionary items (both key and value).
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
- get(): This function returns the value for the given key
- keys(): This function returns a view object that displays a list of all the keys in the dictionary in order of insertion
- values(): This function returns a list of all the values available in a given dictionary
- items(): This function returns the list with all dictionary keys with values
Example: Accessing Dictionary Items.
Python3
# Python program to demonstrate working# of dictionary copydic = {1:'geeks', 2:'for', 3:'geeks'}print('original: ', dic)# Accessing value for keyprint(dic.get(1))# Accessing keys for the dictionaryprint(dic.keys())# Accessing keys for the dictionaryprint(dic.values())# Printing all the items of the Dictionaryprint(dic.items()) |
original: {1: 'geeks', 2: 'for', 3: 'geeks'}
geeks
dict_keys([1, 2, 3])
dict_values(['geeks', 'for', 'geeks'])
dict_items([(1, 'geeks'), (2, 'for'), (3, 'geeks')])Table of Python Dictionary Methods
| Functions Name | Description |
|---|---|
| clear() | Removes all items from the dictionary |
| copy() | Returns a shallow copy of the dictionary |
| fromkeys() | Creates a dictionary from the given sequence |
| get() | Returns the value for the given key |
| items() | Return the list with all dictionary keys with values |
| keys() | Returns a view object that displays a list of all the keys in the dictionary in order of insertion |
| pop() | Returns and removes the element with the given key |
| popitem() | Returns and removes the key-value pair from the dictionary |
| setdefault() | Returns the value of a key if the key is in the dictionary else inserts the key with a value to the dictionary |
| update() | Updates the dictionary with the elements from another dictionary |
| values() | Returns a list of all the values available in a given dictionary |
Note: For more information on Python Dictionary refer to Python Dictionary Tutorial.


