Dictionary Methods in Python
A dictionary in python is an unordered collection of key-value pairs. These key-value pairs can be manipulated using a variety of dictionary methods that are available in Python. In this article, we will discuss some of the most commonly used dictionary methods in Python.
Creating a Dictionary
Before we dive into the dictionary methods, let’s take a look at how to create a dictionary in Python. There are two ways to create a dictionary – using curly braces {} or using the dict() constructor.
Using Curly Braces
To create a dictionary using curly braces, simply enclose key-value pairs in braces {}. Each key-value pair is separated by a comma. Here’s an example:
# Create a dictionary using curly braces
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict)
Output:
{'name': 'John', 'age': 30, 'city': 'New York'}
Using dict() Constructor
To create a dictionary using the dict() constructor, you can pass it a list of tuples where each tuple contains a key-value pair. Here’s an example:
# Create a dictionary using dict() constructor
my_dict = dict([('name', 'John'), ('age', 30), ('city', 'New York')])
print(my_dict)
Output:
{'name': 'John', 'age': 30, 'city': 'New York'}
Dictionary Methods
Now that we have created a dictionary, let’s take a look at some of the most commonly used dictionary methods in Python.
get()
The get() method is used to get the value of a key from a dictionary. If the key does not exist in the dictionary, it returns None. You can also specify a default value to be returned instead of None. Here’s an example:
# Get the value of a key using get() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.get('name'))
# Get the value of a non-existent key and specify a default value
print(my_dict.get('salary', 0))
Output:
John
0
keys()
The keys() method is used to get a list of all the keys in a dictionary. Here’s an example:
# Get a list of keys using keys() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.keys())
Output:
dict_keys(['name', 'age', 'city'])
values()
The values() method is used to get a list of all the values in a dictionary. Here’s an example:
# Get a list of values using values() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.values())
Output:
dict_values(['John', 30, 'New York'])
items()
The items() method is used to get a list of all the key-value pairs in a dictionary. Each key-value pair is returned as a tuple. Here’s an example:
# Get a list of key-value pairs using items() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.items())
Output:
dict_items([('name', 'John'), ('age', 30), ('city', 'New York')])
update()
The update() method is used to update a dictionary with the key-value pairs from another dictionary or from an iterable of key-value pairs. Here’s an example:
# Update a dictionary using update() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
new_dict = {'salary': 5000, 'city': 'Chicago'}
my_dict.update(new_dict)
print(my_dict)
Output:
{'name': 'John', 'age': 30, 'city': 'Chicago', 'salary': 5000}
pop()
The pop() method is used to remove a key-value pair from a dictionary and return its value. If the key does not exist in the dictionary, it raises a KeyError. You can also specify a default value to be returned instead of raising a KeyError. Here’s an example:
# Remove a key-value pair using pop() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.pop('age'))
print(my_dict)
# Try to remove a non-existent key and specify a default value
print(my_dict.pop('salary', 0))
Output:
30
{'name': 'John', 'city': 'New York'}
0
popitem()
The popitem() method is used to remove and return a random key-value pair from a dictionary. If the dictionary is empty, it raises a KeyError. Here’s an example:
# Remove a random key-value pair using popitem() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.popitem())
print(my_dict)
Output:
('city', 'New York')
{'name': 'John', 'age': 30}
clear()
The clear() method is used to remove all the key-value pairs from a dictionary. Here’s an example:
# Remove all key-value pairs using clear() method
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict.clear()
print(my_dict)
Output:
{}
Conclusion
In this article, we have discussed some of the most commonly used dictionary methods in Python. These methods can be used to manipulate key-value pairs in a dictionary, allowing you to easily retrieve, update, and remove data from the dictionary. By using these methods effectively, you can make your Python programs more powerful and efficient.