Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Dictionary Methods

  • Difficulty Level : Basic
  • Last Updated : 04 Aug, 2021

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).

  • 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 copy
dic = {1:'geeks', 2:'for', 3:'geeks'}
print('original: ', dic)
 
# Accessing value for key
print(dic.get(1))
 
# Accessing keys for the dictionary
print(dic.keys())
 
# Accessing keys for the dictionary
print(dic.values())
 
# Printing all the items of the Dictionary
print(dic.items())
Output
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 NameDescription
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.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!