Python Dictionary Methods – Tutorial with Examples

Python dictionaries are unordered collections of key-value pairs. They are implemented as hash tables and allow you to store and retrieve data efficiently based on the keys. Python dictionaries have several built-in methods that can be used to perform various operations on them. In this article, we’ll explore the most commonly used dictionary methods in Python and provide practical examples to help you get started with these methods.

Python Dictionary Methods

There is a total of 11 Python Dictionary Methods. Each one is explained with a quick example here but we’ve also written a detailed article separately for each of them, which is linked to the title for all of them.

clear()

The clear() method is used to remove all items from a dictionary. It returns an empty dictionary. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}

# Clear the dictionary
d.clear()

# Print the result
print(d)

# Output: {}

copy()

The copy() method is used to create a shallow copy of a dictionary. It returns a new dictionary that contains the same key-value pairs as the original dictionary. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}

# Copy the dictionary
d_copy = d.copy()

# Print the result
print(d_copy)

# Output: {'a': 1, 'b': 2, 'c': 3}

fromkeys()

The fromkeys() method is used to create a new dictionary with the specified keys and values. The keys are passed as an iterable and the values are set to the same value for all keys. For example:

# Define a list of keys
keys = ['a', 'b', 'c']

# Create a dictionary from the list of keys and set the values to 0
d = dict.fromkeys(keys, 0)

# Print the result
print(d)

# Output: {'a': 0, 'b': 0, 'c': 0}

get()

The get() method is used to retrieve the value of a key from a dictionary. If the key is not present in the dictionary, it returns a default value specified as the second argument. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}

# Get the value of key 'b'
value = d.get('b')

# Print the result
print(value)

# Output: 2

# Get the value of key 'd'
value = d.get('d', 0)

# Print the result
print(value)

# Output: 0

items()

The items() method is used to return a view of the dictionary’s key-value pairs as an iterable of tuples. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}

# Get the items of the dictionary
items = d.items()

# Print the result
print(items)

# Output: dict_items([('a', 1), ('b', 2), ('c', 3)])

keys()

The keys() method is used to return a view of the dictionary’s keys as an iterable. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}
# Get the keys of the dictionary
keys = d.keys()

# Print the result
print(keys)

Output: dict_keys(['a', 'b', 'c'])

pop()

The pop() method is used to remove and return the value of a specified key from the dictionary. If the key is not present in the dictionary, a default value can be returned instead by specifying the default value as the second argument. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}
# Pop the value of key 'b'
value = d.pop('b')

# Print the result
print(value)

Output: 2
# Print the updated dictionary
print(d)

Output: {'a': 1, 'c': 3}
# Pop the value of key 'd'
value = d.pop('d', 0)

# Print the result
print(value)

Output: 0

popitem()

The popitem() method is used to remove and return an arbitrary key-value pair from the dictionary. The removed item is arbitrary and determined by the internal implementation of the dictionary. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}
Pop an arbitrary item
item = d.popitem()

Print the result
print(item)

Output: ('a', 1)
Print the updated dictionary
print(d)

Output: {'b': 2, 'c': 3}

update()

The update() method is used to merge one dictionary with another. If the same key is present in both dictionaries, the value from the second dictionary will overwrite the value from the first dictionary. For example:

# Define two dictionaries
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
# Update d1 with d2
d1.update(d2)

# Print the result
print(d1)

Output: {'a': 1, 'b': 3, 'c': 4}

values()

The values() method is used to return a view of the dictionary’s values as an iterable. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}
# Get the values of the dictionary
values = d.values()

# Print the result
print(values)

Output: dict_values([1, 2, 3])

setdefault()

The setdefault() method is used to retrieve the value of a key from a dictionary. If the key is not present in the dictionary, it will add the key with a specified default value to the dictionary and return the default value. For example:

# Define a dictionary
d = {'a': 1, 'b': 2, 'c': 3}
# Get the value of key 'b'
value = d.setdefault('b', 0)

# Print the result
print(value)

Output: 2
# Get the value of key 'd'
value = d.setdefault('d', 0)

# Print the result
print(value)

Output: 0
# Print the updated dictionary
print(d)

Output: {'a': 1, 'b': 2, 'c': 3, 'd': 0}

The article provides a comprehensive overview of the various built-in methods of dictionaries in Python. Each method is explained in detail with a practical example, making it easier for readers to understand how they work. These methods cover various operations that can be performed on dictionaries, such as removing all items, creating a shallow copy, creating a new dictionary with specified keys and values, retrieving values of keys, returning views of the dictionary’s key-value pairs and keys, removing an item with a specified key, updating values, and checking the presence of a key. This article is a great resource for anyone looking to learn more about dictionaries and their methods in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *