Last Updated: January 3, 2026
Dictionaries in Python are like the Swiss Army knives of data structures. They allow you to store data in a flexible way, using key-value pairs to map relationships between items.
Whether you're building a user profile, a configuration file, or simply organizing data, dictionaries are your go-to tool. Let's dive into the basics of dictionaries, how they work, and some practical examples that will help you grasp their power.
At its core, a dictionary is an unordered collection of data values that are stored as pairs of keys and values. Each key is unique, meaning you can’t have duplicate keys within a single dictionary.
Here's a simple example:
In this example, name, age, and city are the keys, while "Alice", 30, and "New York" are their respective values.
What makes dictionaries so powerful is that you can access their values quickly using the keys, which is much faster than searching through a list. This efficient lookup makes dictionaries ideal for scenarios where you need to retrieve data frequently.
Creating a dictionary can be done in several ways. The most common method is using curly braces {}, as shown above. However, you can also use the dict() constructor:
To access a value in a dictionary, you can use the key in square brackets:
You can also use the get() method, which is safer as it returns None (or a default value) instead of raising a KeyError if the key does not exist:
Using the get() method is particularly useful when you're unsure if a key exists, helping you avoid potential runtime errors.
Dictionaries are mutable, meaning you can change their contents. You can add new key-value pairs or modify existing ones. Here’s how:
You can add a new key-value pair simply by assigning a value to a new key:
To modify an existing value, just assign a new value to the existing key:
If you want to remove an item from a dictionary, you can use the del statement or the pop() method. The latter allows you to retrieve the removed value:
One of the most powerful features of dictionaries is the ability to loop through their keys, values, or both. This can be incredibly useful for various applications, such as data processing or reporting.
You can loop through the keys of the dictionary using a simple for loop:
To loop through the values, use the values() method:
If you need both keys and values, use the items() method:
This makes it straightforward to work with both parts of the dictionary, which can be particularly handy when generating reports or processing data.
Dictionaries are versatile and are used in a variety of real-world applications. Here are a few scenarios where dictionaries shine:
In web applications, user profiles typically store data about users. For example, you might have a dictionary that holds a user's information:
Configuration settings for applications can also be represented as dictionaries. This allows you to easily manage settings without dealing with complex structures:
Dictionaries can be used to count occurrences of items in a list. For example, if you have a list of words and want to count how many times each word appears, you could do something like this:
In this example, we use the dictionary to keep track of how many times each word appears, showcasing a common use case for dictionaries.
While dictionaries are powerful, there are some common pitfalls to be aware of:
Dictionaries require immutable types as keys. This means you cannot use lists or other dictionaries as keys:
Prior to Python 3.7, dictionaries did not maintain the order of items. However, from Python 3.7 onwards, dictionaries preserve insertion order. This means you can rely on the order of items when iterating through them.
Dictionaries can consume more memory compared to lists due to their underlying hash table implementation. Keep this in mind if you're working with a large dataset.
Always use the get() method when you're not sure if a key exists to avoid KeyError. This small practice can save you a lot of debugging time.
Now that you understand the basics of dictionaries, including how to create, access, modify, and utilize them in real-world scenarios, you're ready to explore the next level of functionality.
In the next chapter, we will look at dictionary methods, where you'll learn about the built-in capabilities that can make your work with dictionaries even easier and more efficient.