Skip to content

Dictionary Methods

Dictionary Methods

In Python, a dictionary is a collection of key-value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key. A key’s value can be a number, a string, a list, or even another dictionary. In fact, you can use any object that you can create in Python as a value in a dictionary. We have a set of methods that can be used with dictionaries. We will cover the following methods - clear, copy, fromkeys, get, items, keys, pop, popitem, setdefault, update, and values.

Table of Methods

S.No.MethodDescriptionExample
1clear()Removes all the elements from the dictionarymy_dict.clear()my_dict.clear()
2copy()Returns a copy of the dictionarymy_dict.copy()my_dict.copy()
3fromkeys()Returns a dictionary with the specified keys and valuemy_dict.fromkeys(keys, value)my_dict.fromkeys(keys, value)
4get()Returns the value of the specified keymy_dict.get(key)my_dict.get(key)
5items()Returns a list containing a tuple for each key value pairmy_dict.items()my_dict.items()
6keys()Returns a list containing the dictionary’s keysmy_dict.keys()my_dict.keys()
7pop()Removes the element with the specified keymy_dict.pop(key)my_dict.pop(key)
8popitem()Removes the last inserted key-value pairmy_dict.popitem()my_dict.popitem()
9setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified valuemy_dict.setdefault(key, value)my_dict.setdefault(key, value)
10update()Updates the dictionary with the specified key-value pairsmy_dict.update(key, value)my_dict.update(key, value)
11values()Returns a list of all the values in the dictionarymy_dict.values()my_dict.values()
12has_key()Returns True if a key exists in the dictionarymy_dict.has_key(key)my_dict.has_key(key)
13len()Returns the number of items in the dictionarylen(my_dict)len(my_dict)
14delRemoves the item with the specified keydel my_dict[key]del my_dict[key]
15inReturns True if a key exists in the dictionarykey in my_dictkey in my_dict
16not inReturns True if a key does not exist in the dictionarykey not in my_dictkey not in my_dict

clear()

The clear()clear() method removes all the elements from a dictionary.

clear.py
data = {'a': 1, 'b': 2, 'c': 3}
data.clear()
print(data)
clear.py
data = {'a': 1, 'b': 2, 'c': 3}
data.clear()
print(data)

Output:

command
C:\Users\username>python clear.py
{}
command
C:\Users\username>python clear.py
{}

In this example, we declare a dictionary and assign it to the variable datadata. We then use the clear()clear() method to remove all the elements from the dictionary. The output shows that the dictionary is empty.

copy()

The copy()copy() method returns a copy of the specified dictionary.

copy.py
data = {'a': 1, 'b': 2, 'c': 3}
new_data = data.copy()
print(new_data)
copy.py
data = {'a': 1, 'b': 2, 'c': 3}
new_data = data.copy()
print(new_data)

Output:

command
C:\Users\username>python copy.py
{'a': 1, 'b': 2, 'c': 3}
command
C:\Users\username>python copy.py
{'a': 1, 'b': 2, 'c': 3}

In this example, we declare a dictionary and assign it to the variable datadata. We then use the copy()copy() method to create a copy of the dictionary and assign it to the variable new_datanew_data. The output shows that the dictionary is copied.

fromkeys()

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

fromkeys.py
keys = ('a', 'b', 'c')
value = 0
data = dict.fromkeys(keys, value)
print(data)
fromkeys.py
keys = ('a', 'b', 'c')
value = 0
data = dict.fromkeys(keys, value)
print(data)

Output:

command
C:\Users\username>python fromkeys.py
{'a': 0, 'b': 0, 'c': 0}
command
C:\Users\username>python fromkeys.py
{'a': 0, 'b': 0, 'c': 0}

In this example, we declare a tuple and assign it to the variable keyskeys. We then declare a variable valuevalue and assign it the value 00. We then use the fromkeys()fromkeys() method to create a dictionary with the specified keys and value and assign it to the variable datadata. The output shows that the dictionary is created.

get()

The get()get() method returns the value of the specified key.

get.py
data = {'a': 1, 'b': 2, 'c': 3}
value = data.get('a')
print(value)
get.py
data = {'a': 1, 'b': 2, 'c': 3}
value = data.get('a')
print(value)

Output:

command
C:\Users\username>python get.py
1
command
C:\Users\username>python get.py
1

In this example, we declare a dictionary and assign it to the variable datadata. We then use the get()get() method to get the value of the key aa and assign it to the variable valuevalue. The output shows that the value is returned.

items()

The items()items() method returns a list containing a tuple for each key value pair.

items.py
data = {'a': 1, 'b': 2, 'c': 3}
items = data.items()
print(items)
items.py
data = {'a': 1, 'b': 2, 'c': 3}
items = data.items()
print(items)

Output:

command
C:\Users\username>python items.py
dict_items([('a', 1), ('b', 2), ('c', 3)])
command
C:\Users\username>python items.py
dict_items([('a', 1), ('b', 2), ('c', 3)])

In this example, we declare a dictionary and assign it to the variable datadata. We then use the items()items() method to get the items of the dictionary and assign it to the variable itemsitems. The output shows that the items are returned.

keys()

The keys()keys() method returns a list containing the dictionary’s keys.

keys.py
data = {'a': 1, 'b': 2, 'c': 3}
keys = data.keys()
print(keys)
keys.py
data = {'a': 1, 'b': 2, 'c': 3}
keys = data.keys()
print(keys)

Output:

command
C:\Users\username>python keys.py
dict_keys(['a', 'b', 'c'])
command
C:\Users\username>python keys.py
dict_keys(['a', 'b', 'c'])

In this example, we declare a dictionary and assign it to the variable datadata. We then use the keys()keys() method to get the keys of the dictionary and assign it to the variable keyskeys. The output shows that the keys are returned.

pop()

The pop()pop() method removes the element with the specified key.

pop.py
data = {'a': 1, 'b': 2, 'c': 3}
data.pop('a')
print(data)
pop.py
data = {'a': 1, 'b': 2, 'c': 3}
data.pop('a')
print(data)

Output:

command
C:\Users\username>python pop.py
{'b': 2, 'c': 3}
command
C:\Users\username>python pop.py
{'b': 2, 'c': 3}

In this example, we declare a dictionary and assign it to the variable datadata. We then use the pop()pop() method to remove the element with the key aa. The output shows that the element is removed.

popitem()

The popitem()popitem() method removes the last inserted key-value pair.

popitem.py
data = {'a': 1, 'b': 2, 'c': 3}
data.popitem()
print(data)
popitem.py
data = {'a': 1, 'b': 2, 'c': 3}
data.popitem()
print(data)

Output:

command
C:\Users\username>python popitem.py
{'a': 1, 'b': 2}
command
C:\Users\username>python popitem.py
{'a': 1, 'b': 2}

In this example, we declare a dictionary and assign it to the variable datadata. We then use the popitem()popitem() method to remove the last inserted key-value pair. The output shows that the key-value pair is removed.

setdefault()

The setdefault()setdefault() method returns the value of the specified key. If the key does not exist: insert the key, with the specified value.

setdefault.py
data = {'a': 1, 'b': 2, 'c': 3}
value = data.setdefault('a', 0)
print(value)
value = data.setdefault('d', 4)
print(value)
print(data)
setdefault.py
data = {'a': 1, 'b': 2, 'c': 3}
value = data.setdefault('a', 0)
print(value)
value = data.setdefault('d', 4)
print(value)
print(data)

Output:

command
C:\Users\username>python setdefault.py
1
4
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
command
C:\Users\username>python setdefault.py
1
4
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

In this example, we declare a dictionary and assign it to the variable datadata. We then use the setdefault()setdefault() method to get the value of the key aa and assign it to the variable valuevalue. The output shows that the value is returned. We then use the setdefault()setdefault() method to get the value of the key dd and assign it to the variable valuevalue. The output shows that the value is returned and the key-value pair is added to the dictionary.

update()

The update()update() method updates the dictionary with the specified key-value pairs.

update.py
data = {'a': 1, 'b': 2, 'c': 3}
data.update({'d': 4, 'e': 5})
print(data)
update.py
data = {'a': 1, 'b': 2, 'c': 3}
data.update({'d': 4, 'e': 5})
print(data)

Output:

command
C:\Users\username>python update.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
command
C:\Users\username>python update.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

In this example, we declare a dictionary and assign it to the variable datadata. We then use the update()update() method to update the dictionary with the specified key-value pairs. The output shows that the dictionary is updated.

values()

The values()values() method returns a list of all the values in the dictionary.

values.py
data = {'a': 1, 'b': 2, 'c': 3}
values = data.values()
print(values)
values.py
data = {'a': 1, 'b': 2, 'c': 3}
values = data.values()
print(values)

Output:

command
C:\Users\username>python values.py
dict_values([1, 2, 3])
command
C:\Users\username>python values.py
dict_values([1, 2, 3])

In this example, we declare a dictionary and assign it to the variable datadata. We then use the values()values() method to get the values of the dictionary and assign it to the variable valuesvalues. The output shows that the values are returned.

has_key()

The has_key()has_key() method returns True if a key exists in the dictionary.

has_key.py
data = {'a': 1, 'b': 2, 'c': 3}
print(data.has_key('a'))
print(data.has_key('d'))
has_key.py
data = {'a': 1, 'b': 2, 'c': 3}
print(data.has_key('a'))
print(data.has_key('d'))

Output:

command
C:\Users\username>python has_key.py
True
False
command
C:\Users\username>python has_key.py
True
False

In this example, we declare a dictionary and assign it to the variable datadata. We then use the has_key()has_key() method to check if the key aa exists in the dictionary. The output shows that the key exists. We then use the has_key()has_key() method to check if the key dd exists in the dictionary. The output shows that the key does not exist.

len()

The len()len() method returns the number of items in the dictionary.

len.py
data = {'a': 1, 'b': 2, 'c': 3}
print(len(data))
len.py
data = {'a': 1, 'b': 2, 'c': 3}
print(len(data))

Output:

command
C:\Users\username>python len.py
3
command
C:\Users\username>python len.py
3

In this example, we declare a dictionary and assign it to the variable datadata. We then use the len()len() method to get the number of items in the dictionary. The output shows that the number of items is returned.

del

The deldel keyword removes the item with the specified key.

del.py
data = {'a': 1, 'b': 2, 'c': 3}
del data['a']
print(data)
del.py
data = {'a': 1, 'b': 2, 'c': 3}
del data['a']
print(data)

Output:

command
C:\Users\username>python del.py
{'b': 2, 'c': 3}
command
C:\Users\username>python del.py
{'b': 2, 'c': 3}

In this example, we declare a dictionary and assign it to the variable datadata. We then use the deldel keyword to remove the item with the key aa. The output shows that the item is removed.

in

The inin keyword returns True if a key exists in the dictionary.

in.py
data = {'a': 1, 'b': 2, 'c': 3}
print('a' in data)
print('d' in data)
in.py
data = {'a': 1, 'b': 2, 'c': 3}
print('a' in data)
print('d' in data)

Output:

command
C:\Users\username>python in.py
True
False
command
C:\Users\username>python in.py
True
False

In this example, we declare a dictionary and assign it to the variable datadata. We then use the inin keyword to check if the key aa exists in the dictionary. The output shows that the key exists. We then use the inin keyword to check if the key dd exists in the dictionary. The output shows that the key does not exist.

not in

The not innot in keyword returns True if a key does not exist in the dictionary.

not_in.py
data = {'a': 1, 'b': 2, 'c': 3}
print('a' not in data)
print('d' not in data)
not_in.py
data = {'a': 1, 'b': 2, 'c': 3}
print('a' not in data)
print('d' not in data)

Output:

command
C:\Users\username>python not_in.py
False
True
command
C:\Users\username>python not_in.py
False
True

In this example, we declare a dictionary and assign it to the variable datadata. We then use the not innot in keyword to check if the key aa does not exist in the dictionary. The output shows that the key exists. We then use the not innot in keyword to check if the key dd does not exist in the dictionary. The output shows that the key does not exist.

Conclusion

In this tutorial, we have covered the methods available for dictionaries in Python. We have covered the following methods - clear, copy, fromkeys, get, items, keys, pop, popitem, setdefault, update, and values. For more information on dictionaries, you can refer to the official documentation. For more tutorials on Python, you can refer to the Python Central Hub.

Was this page helpful?

Let us know how we did