dictionary

In Python, a dictionary is a built-in data type that allows you to store a collection of key-value pairs. Dictionaries are mutable, which means you can change, add, or remove key-value pairs after the dictionary has been created.

In Python versions prior to 3.7 dictionaries were unordered data types, meaning they don’t maintain any order of key-value pairs. However, starting from Python 3.7, dictionaries maintain the insertion order of items.

Dictionaries are highly efficient for data retrieval because they use a hash table under the hood, allowing you to access values quickly if you know the corresponding key. This makes them particularly useful for representing structured data, such as JSON objects or any dataset where you need to associate unique keys with values.

Example

Here’s an example of how to use a dictionary in Python:

Python
>>> person = {
...     "name": "Alice",
...     "age": 30,
...     "city": "New York"
... }

>>> # Accessing a value
>>> person["name"]
Alice

>>> # Adding a new key-value pair
>>> person["email"] = "alice@example.com"

>>> # Updating a value
>>> person["age"] = 31

>>> # Removing a key-value pair
>>> del person["city"]

In this example, you create a person dictionary using a literal. Dictionary literals consist of a pair of curly braces containing comma-separated key-value pairs where keys and values are separated by a colon. Then, you perform some actions like accessing a value through its associated key, adding a new key-value pair, and updating and removing existing pairs.

Tutorial

Dictionaries in Python

In this tutorial, you'll learn how to work with Python dictionaries to help you process data more efficiently. You'll learn how to create dictionaries, access their keys and values, update dictionaries, and more.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 6, 2025 • Reviewed by Philipp Acsany