Back to: Python Programming
A dictionary in Python is a collection of key-value pairs. It is a mutable data structure that is used to store and retrieve data efficiently. In this tutorial, we will cover the basics of Python dictionaries, including how to create, access, modify, and delete dictionary items.
Creating a dictionary
To create a dictionary in Python, you can use curly braces {} and separate the key-value pairs with a colon (:). For example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Alternatively, you can use the built-in dict() function. For example:
my_dict = dict(key1='value1', key2='value2', key3='value3')
Both methods produce the same result, which is a dictionary with three key-value pairs.
Accessing dictionary items
To access a value in a dictionary, you can use square brackets [] and specify the key. For example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
print(my_dict['key2']) # Output: 'value2'
If you try to access a key that doesn’t exist in the dictionary, you will get a KeyError. To avoid this, you can use the get() method instead. For example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
print(my_dict.get('key4', 'default')) # Output: 'default'
This will return the default value (‘default’) instead of raising a KeyError.
Modifying dictionary items
To modify a value in a dictionary, you can simply assign a new value to the corresponding key. For example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
my_dict['key2'] = 'new_value'
print(my_dict) # Output: {'key1': 'value1', 'key2': 'new_value', 'key3': 'value3'}
If the key doesn’t exist, it will be created with the new value. For example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
my_dict['key4'] = 'new_value'
print(my_dict) # Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'new_value'}
Deleting dictionary items
To delete a key-value pair from a dictionary, you can use the del keyword and specify the key. For example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
del my_dict['key2']
print(my_dict) # Output: {'key1': 'value1', 'key3': 'value3'}
You can also use the pop() method to remove a key-value pair and return the value. For example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
value = my_dict.pop('key2', 'default')
print(my_dict) # Output: {'key1': 'value1', 'key3': 'value3'}
print(value) # Output: 'value2'
This will remove the key-value pair (‘key2’, ‘value2’) and return the value (‘value2’).
Python Dictionary Summary Table:
Concept | Syntax | Example | Explanation |
---|---|---|---|
Creating a Dictionary | {key1: value1, key2: value2} or dict(key1=value1, key2=value2) | my_dict = {'name': 'John', 'age': 30} | Creates a dictionary with two key-value pairs |
Accessing Values | my_dict[key] or my_dict.get(key, default) | my_dict['name'] or my_dict.get('address', 'unknown') | Accesses the value associated with the key ‘name’ or returns the default value ‘unknown’ if the key ‘address’ is not found |
Modifying Values | my_dict[key] = new_value | my_dict['age'] = 40 | Modifies the value associated with the key ‘age’ to 40 |
Adding Key-Value Pairs | my_dict[new_key] = new_value | my_dict['address'] = '123 Main St' | Adds a new key-value pair ‘address’ and its corresponding value ‘123 Main St’ to the dictionary |
Removing Key-Value Pairs | del my_dict[key] or my_dict.pop(key, default) | del my_dict['age'] or my_dict.pop('address', 'unknown') | Removes the key-value pair associated with the key ‘age’ or returns the value associated with the key ‘address’ and removes the key-value pair from the dictionary |
This chart summarizes the basic concepts of Python Dictionary and provides examples for each concept.
Code example on Python Dictionary:
# Creating a dictionary
my_dict = {'name': 'John', 'age': 30, 'address': '123 Main St'}
# Accessing values
print(my_dict['name']) # Output: 'John'
print(my_dict.get('phone', 'unknown')) # Output: 'unknown'
# Modifying values
my_dict['age'] = 40
print(my_dict) # Output: {'name': 'John', 'age': 40, 'address': '123 Main St'}
# Adding key-value pairs
my_dict['phone'] = '555-1234'
print(my_dict) # Output: {'name': 'John', 'age': 40, 'address': '123 Main St', 'phone': '555-1234'}
# Removing key-value pairs
del my_dict['address']
print(my_dict) # Output: {'name': 'John', 'age': 40, 'phone': '555-1234'}
value = my_dict.pop('phone', 'unknown')
print(value) # Output: '555-1234'
print(my_dict) # Output: {'name': 'John', 'age': 40}
This code example demonstrates how to create, access, modify, add, and remove key-value pairs from a dictionary in Python. It also shows how to use the get()
and pop()
methods to handle missing keys and retrieve values while removing the key-value pair. This code can be used as a reference or a starting point for working with dictionaries in Python.
Follow us on social media
Follow Author