Python provides various data structures to work with complex data. One of these data structures is the dictionary. A dictionary is a mutable, unordered collection of key-value pairs, where each key is unique and associated with a corresponding value. In this tutorial, we will discuss Python dictionary keys and their usage.
What are Python Keys?
In Python, a key is a unique identifier used to access the corresponding value in the dictionary. Dictionary keys must be immutable (unchangeable) objects like numbers, strings, or tuples. Immutability is required because keys are used to hash the values in the dictionary, and a mutable key would result in inconsistent behavior due to the possibility of changing hash values.
How to Use Python Keys?
Here are five examples illustrating how to use Python keys when working with dictionaries:
Example 1: Creating a Dictionary
To create a dictionary in Python, you can use the curly braces {}
or the built-in dict()
function. Here is an example:
# Using curly braces
my_dict = {'apple': 3, 'banana': 5, 'cherry': 7}
# Using the dict() function
my_dict = dict(apple=3, banana=5, cherry=7)
In the examples above, we have created a dictionary with three key-value pairs. The keys are strings, and the values are integers.
Example 2: Accessing Values using Keys
To access the values in a dictionary, you can use the keys as shown below:
my_dict = {'apple': 3, 'banana': 5, 'cherry': 7}
print(my_dict['apple']) # Output: 3
In the example above, we have accessed the value of the key 'apple'
in the dictionary my_dict
.
Example 3: Updating Values using Keys
To update values in a dictionary, you can use the keys. Here is an example:
my_dict = {'apple': 3, 'banana': 5, 'cherry': 7}
my_dict['apple'] = 4
print(my_dict) # Output: {'apple': 4, 'banana': 5, 'cherry': 7}
In the example above, we have updated the value of the key 'apple'
in the dictionary my_dict
.
Example 4: Adding New Key-Value Pairs
To add new key-value pairs to a dictionary, you can use the keys. Here is an example:
my_dict = {'apple': 3, 'banana': 5, 'cherry': 7}
my_dict['orange'] = 9
print(my_dict) # Output: {'apple': 3, 'banana': 5, 'cherry': 7, 'orange': 9}
In the example above, we have added a new key-value pair ('orange': 9
) to the dictionary my_dict
.
Example 5: Deleting Key-Value Pairs
To delete key-value pairs from a dictionary, you can use the del
keyword followed by the key. Here is an example:
my_dict = {'apple': 3, 'banana': 5, 'cherry': 7}
del my_dict['banana']
print(my_dict) # Output: {'apple': 3, 'cherry': 7}
In the example above, we have deleted the key-value pair with the key 'banana'
from the dictionary my_dict
.
Conclusion
In this tutorial, we have discussed Python dictionary keys and how to use them for various operations. We have seen that keys are unique identifiers and are used to access the corresponding values in a dictionary. We have also demonstrated how to create, access, update, add, and delete key-value pairs in a dictionary. By understanding and applying these concepts, you can create powerful Python applications that store and manipulate data efficiently.