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. | Method | Description | Example |
---|---|---|---|
1 | clear() | Removes all the elements from the dictionary | my_dict.clear() my_dict.clear() |
2 | copy() | Returns a copy of the dictionary | my_dict.copy() my_dict.copy() |
3 | fromkeys() | Returns a dictionary with the specified keys and value | my_dict.fromkeys(keys, value) my_dict.fromkeys(keys, value) |
4 | get() | Returns the value of the specified key | my_dict.get(key) my_dict.get(key) |
5 | items() | Returns a list containing a tuple for each key value pair | my_dict.items() my_dict.items() |
6 | keys() | Returns a list containing the dictionary’s keys | my_dict.keys() my_dict.keys() |
7 | pop() | Removes the element with the specified key | my_dict.pop(key) my_dict.pop(key) |
8 | popitem() | Removes the last inserted key-value pair | my_dict.popitem() my_dict.popitem() |
9 | setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value | my_dict.setdefault(key, value) my_dict.setdefault(key, value) |
10 | update() | Updates the dictionary with the specified key-value pairs | my_dict.update(key, value) my_dict.update(key, value) |
11 | values() | Returns a list of all the values in the dictionary | my_dict.values() my_dict.values() |
12 | has_key() | Returns True if a key exists in the dictionary | my_dict.has_key(key) my_dict.has_key(key) |
13 | len() | Returns the number of items in the dictionary | len(my_dict) len(my_dict) |
14 | del | Removes the item with the specified key | del my_dict[key] del my_dict[key] |
15 | in | Returns True if a key exists in the dictionary | key in my_dict key in my_dict |
16 | not in | Returns True if a key does not exist in the dictionary | key not in my_dict key not in my_dict |
clear()
The clear()
clear()
method removes all the elements from a dictionary.
data = {'a': 1, 'b': 2, 'c': 3}
data.clear()
print(data)
data = {'a': 1, 'b': 2, 'c': 3}
data.clear()
print(data)
Output:
C:\Users\username>python clear.py
{}
C:\Users\username>python clear.py
{}
In this example, we declare a dictionary and assign it to the variable data
data
. 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.
data = {'a': 1, 'b': 2, 'c': 3}
new_data = data.copy()
print(new_data)
data = {'a': 1, 'b': 2, 'c': 3}
new_data = data.copy()
print(new_data)
Output:
C:\Users\username>python copy.py
{'a': 1, 'b': 2, 'c': 3}
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 data
data
. We then use the copy()
copy()
method to create a copy of the dictionary and assign it to the variable new_data
new_data
. The output shows that the dictionary is copied.
fromkeys()
The fromkeys()
fromkeys()
method returns a dictionary with the specified keys and value.
keys = ('a', 'b', 'c')
value = 0
data = dict.fromkeys(keys, value)
print(data)
keys = ('a', 'b', 'c')
value = 0
data = dict.fromkeys(keys, value)
print(data)
Output:
C:\Users\username>python fromkeys.py
{'a': 0, 'b': 0, 'c': 0}
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 keys
keys
. We then declare a variable value
value
and assign it the value 0
0
. We then use the fromkeys()
fromkeys()
method to create a dictionary with the specified keys and value and assign it to the variable data
data
. The output shows that the dictionary is created.
get()
The get()
get()
method returns the value of the specified key.
data = {'a': 1, 'b': 2, 'c': 3}
value = data.get('a')
print(value)
data = {'a': 1, 'b': 2, 'c': 3}
value = data.get('a')
print(value)
Output:
C:\Users\username>python get.py
1
C:\Users\username>python get.py
1
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the get()
get()
method to get the value of the key a
a
and assign it to the variable value
value
. The output shows that the value is returned.
items()
The items()
items()
method returns a list containing a tuple for each key value pair.
data = {'a': 1, 'b': 2, 'c': 3}
items = data.items()
print(items)
data = {'a': 1, 'b': 2, 'c': 3}
items = data.items()
print(items)
Output:
C:\Users\username>python items.py
dict_items([('a', 1), ('b', 2), ('c', 3)])
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 data
data
. We then use the items()
items()
method to get the items of the dictionary and assign it to the variable items
items
. The output shows that the items are returned.
keys()
The keys()
keys()
method returns a list containing the dictionary’s keys.
data = {'a': 1, 'b': 2, 'c': 3}
keys = data.keys()
print(keys)
data = {'a': 1, 'b': 2, 'c': 3}
keys = data.keys()
print(keys)
Output:
C:\Users\username>python keys.py
dict_keys(['a', 'b', 'c'])
C:\Users\username>python keys.py
dict_keys(['a', 'b', 'c'])
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the keys()
keys()
method to get the keys of the dictionary and assign it to the variable keys
keys
. The output shows that the keys are returned.
pop()
The pop()
pop()
method removes the element with the specified key.
data = {'a': 1, 'b': 2, 'c': 3}
data.pop('a')
print(data)
data = {'a': 1, 'b': 2, 'c': 3}
data.pop('a')
print(data)
Output:
C:\Users\username>python pop.py
{'b': 2, 'c': 3}
C:\Users\username>python pop.py
{'b': 2, 'c': 3}
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the pop()
pop()
method to remove the element with the key a
a
. The output shows that the element is removed.
popitem()
The popitem()
popitem()
method removes the last inserted key-value pair.
data = {'a': 1, 'b': 2, 'c': 3}
data.popitem()
print(data)
data = {'a': 1, 'b': 2, 'c': 3}
data.popitem()
print(data)
Output:
C:\Users\username>python popitem.py
{'a': 1, 'b': 2}
C:\Users\username>python popitem.py
{'a': 1, 'b': 2}
In this example, we declare a dictionary and assign it to the variable data
data
. 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.
data = {'a': 1, 'b': 2, 'c': 3}
value = data.setdefault('a', 0)
print(value)
value = data.setdefault('d', 4)
print(value)
print(data)
data = {'a': 1, 'b': 2, 'c': 3}
value = data.setdefault('a', 0)
print(value)
value = data.setdefault('d', 4)
print(value)
print(data)
Output:
C:\Users\username>python setdefault.py
1
4
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
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 data
data
. We then use the setdefault()
setdefault()
method to get the value of the key a
a
and assign it to the variable value
value
. The output shows that the value is returned. We then use the setdefault()
setdefault()
method to get the value of the key d
d
and assign it to the variable value
value
. 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.
data = {'a': 1, 'b': 2, 'c': 3}
data.update({'d': 4, 'e': 5})
print(data)
data = {'a': 1, 'b': 2, 'c': 3}
data.update({'d': 4, 'e': 5})
print(data)
Output:
C:\Users\username>python update.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
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 data
data
. 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.
data = {'a': 1, 'b': 2, 'c': 3}
values = data.values()
print(values)
data = {'a': 1, 'b': 2, 'c': 3}
values = data.values()
print(values)
Output:
C:\Users\username>python values.py
dict_values([1, 2, 3])
C:\Users\username>python values.py
dict_values([1, 2, 3])
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the values()
values()
method to get the values of the dictionary and assign it to the variable values
values
. 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.
data = {'a': 1, 'b': 2, 'c': 3}
print(data.has_key('a'))
print(data.has_key('d'))
data = {'a': 1, 'b': 2, 'c': 3}
print(data.has_key('a'))
print(data.has_key('d'))
Output:
C:\Users\username>python has_key.py
True
False
C:\Users\username>python has_key.py
True
False
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the has_key()
has_key()
method to check if the key a
a
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 d
d
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.
data = {'a': 1, 'b': 2, 'c': 3}
print(len(data))
data = {'a': 1, 'b': 2, 'c': 3}
print(len(data))
Output:
C:\Users\username>python len.py
3
C:\Users\username>python len.py
3
In this example, we declare a dictionary and assign it to the variable data
data
. 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 del
del
keyword removes the item with the specified key.
data = {'a': 1, 'b': 2, 'c': 3}
del data['a']
print(data)
data = {'a': 1, 'b': 2, 'c': 3}
del data['a']
print(data)
Output:
C:\Users\username>python del.py
{'b': 2, 'c': 3}
C:\Users\username>python del.py
{'b': 2, 'c': 3}
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the del
del
keyword to remove the item with the key a
a
. The output shows that the item is removed.
in
The in
in
keyword returns True if a key exists in the dictionary.
data = {'a': 1, 'b': 2, 'c': 3}
print('a' in data)
print('d' in data)
data = {'a': 1, 'b': 2, 'c': 3}
print('a' in data)
print('d' in data)
Output:
C:\Users\username>python in.py
True
False
C:\Users\username>python in.py
True
False
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the in
in
keyword to check if the key a
a
exists in the dictionary. The output shows that the key exists. We then use the in
in
keyword to check if the key d
d
exists in the dictionary. The output shows that the key does not exist.
not in
The not in
not in
keyword returns True if a key does not exist in the dictionary.
data = {'a': 1, 'b': 2, 'c': 3}
print('a' not in data)
print('d' not in data)
data = {'a': 1, 'b': 2, 'c': 3}
print('a' not in data)
print('d' not in data)
Output:
C:\Users\username>python not_in.py
False
True
C:\Users\username>python not_in.py
False
True
In this example, we declare a dictionary and assign it to the variable data
data
. We then use the not in
not in
keyword to check if the key a
a
does not exist in the dictionary. The output shows that the key exists. We then use the not in
not in
keyword to check if the key d
d
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