How To Get An Item From A Dictionary In Python

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:

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:

The output should be:


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:

The output should be:


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:

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

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.


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.