In this tutorial, we will learn about dictionaries in Python and how to retrieve items from them. A dictionary is a mutable, unordered collection of key-value pairs, where each key must be unique.
Dictionaries are suitable for cases where you’d want to look up data using a key instead of an index. We will discuss step-by-step how to get an item from a dictionary in Python.
Step 1: Creating a Dictionary
First, let’s create a simple dictionary to work with. In Python, you can create dictionaries using curly braces {}
and separate keys and their corresponding values using colons :
. Here’s an example of a dictionary that stores the ages of different people:
1 2 3 4 5 6 |
ages = { "Alice": 30, "Bob": 25, "Charlie": 22, "David": 28 } |
Here, the names of the people are the keys, and their ages are the corresponding values.
Step 2: Accessing an Item Using Square Brackets
You can access an item in a dictionary by placing the key inside square brackets []
. For example, to get Alice’s age, you would do:
1 2 |
alice_age = ages["Alice"] print(alice_age) |
The output should be:
30
However, using square brackets can lead to a KeyError if the key is not found in the dictionary.
Step 3: Accessing an Item Using the get
Method
To avoid the KeyError, you can use the get()
method to access an item in the dictionary. The get()
method returns the value for the given key if it exists, or a specified default value if the key is not found. For example:
1 2 3 4 5 |
alice_age = ages.get("Alice", 0) print(alice_age) unknown_age = ages.get("Unknown", 0) print(unknown_age) |
The output should be:
30 0
In this example, “Unknown” is not a key in the dictionary, so the get()
method returns the default value 0
.
Step 4: Iterating Through a Dictionary
You can also iterate through a dictionary using a for loop. By default, iterating through a dictionary will give you its keys. To access the corresponding value, you can use the key inside square brackets, or the get()
method. Here’s an example:
1 2 3 |
for name in ages: age = ages[name] print(f"{name} is {age} years old.") |
The output should be:
Alice is 30 years old. Bob is 25 years old. Charlie is 22 years old. David is 28 years old.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
ages = { "Alice": 30, "Bob": 25, "Charlie": 22, "David": 28 } alice_age = ages["Alice"] print(alice_age) alice_age = ages.get("Alice", 0) print(alice_age) unknown_age = ages.get("Unknown", 0) print(unknown_age) for name in ages: age = ages[name] print(f"{name} is {age} years old.") |
In this tutorial, we learned how to create a dictionary, access items using square brackets and the get()
method, and how to iterate through a dictionary. These skills will prove beneficial when working with data structures and organizing information in Python.
Conclusion
Dictionaries are versatile and essential data structures in Python, facilitating the organization and retrieval of data using keys. Understanding how to access items in dictionaries is crucial when working with Python applications. Now you know the basic methods to access items in dictionaries, making your programming tasks more efficient and effective.