Python Dictionary Methods with Example

Python dictionary methods; Through this tutorial, i am going to show you how many methods available in python dictionary. And how to use it.

Python Dictionary Functions/Methods

The list of built-in dictionary methods/functions in python; as shown in below list:

MethodDescription
clear()Removes all the elements from the dictionary
copy()Returns a copy of the dictionary
fromkeys()Returns a dictionary with the specified keys and value
get()Returns the value of the specified key
items()Returns a list containing a tuple for each key value pair
keys()Returns a list containing the dictionary’s keys
pop()Removes the element with the specified key
popitem()Removes the last inserted key-value pair
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update()Updates the dictionary with the specified key-value pairs
values()Returns a list of all the values in the dictionary

Clear Dictionary Method in Python

The clear() method is used to remove all the elements/items from the python dictionary.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
my_dict.clear()
print(my_dict)
#output { }

Python Copy dictionary Method

The copy() method is used to a copy of the specified python dictionary:

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
x = my_dict.copy()
print(x)
#output {'name': 'john', 'age': '28'}

Dictionary fromkeys Method in Python

The fromkeys() method used to a dictionary with the specified keys and the specified value.

x = ('key1', 'key2', 'key3')
y = 0
my_dict = dict.fromkeys(x, y)
print(my_dict)
#{'key1': 0, 'key2': 0, 'key3': 0}

Python dictionary get method

The get() method is used to get the value of the item with the specified key.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
x = my_dict.get('name')
print(x)
#output john

Python dictionary items method

The items() method returns a view object. The view object contains the key-value pairs of the dictionary, as tuples in a list.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
x = my_dict.items()
print(x)
#output dict_items([('name', 'john'), ('age', '28')])

Python dictionary keys method

The keys() method returns a view object. The view object contains the keys of the dictionary, as a list.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
x = my_dict.keys()
print(x)
#output dict_keys(['name', 'age'])

Dictionary pop method python

The pop() method is used to remove the specified item from the dictionary.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
x = my_dict.pop('name')
print(x)
#output john

Python Dictionary popitem() Method

The popitem() method used to remove the item that was last inserted into the dictionary.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
x = my_dict.popitem()
print(x)
#output ('age', '28')

Python Dictionary update() Method

The python update() method inserts the specified items to the dictionary. The specified items can be a dictionary, or an iterable object with key value pairs.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
my_dict.update({"fav_color": "White"})
print(my_dict)
#output {'name': 'john', 'age': '28', 'fav_color': 'White'}

Python Dictionary values() Method

The values() method is used to return a view object. The view object contains the values of the dictionary, as a list.

# python dictionary
my_dict = {'name': 'john', 'age': '28'}
x = my_dict.values()
print(x)
#output dict_values(['john', '28'])

Recommended Python Tutorials

Recommended:-Python Strings

Be First to Comment

Leave a Reply

Your email address will not be published.