Python Progamming/Python Data Structures

Python Dictionaries

Updated 3/15/2026
1 min read

In the previous article, you learned about sets, a data structure that stores collections of unique elements. Sets are useful when a program needs to ensure that values do not repeat and when efficient membership checks are important. However, many programming tasks involve not only storing values but also associating one piece of information with another.

For example, a program might need to connect a user ID with a username, a product name with its price, or a country with its capital city. In these cases, storing values alone is not enough; the program needs a way to map one value to another.

Python provides a data structure designed specifically for this purpose: the dictionary.

What Is a Dictionary?

A dictionary is a data structure that stores data in key–value pairs. Each key acts as an identifier that maps to a corresponding value.

For example:

user = {
    "name": "Mohan",
    "age": 21,
    "role": "Developer"
}

In this example, "name", "age", and "role" are keys, while "Mohan", 21, and "Developer" are the associated values.

Dictionaries allow programs to represent structured information clearly and efficiently.

Accessing Values in a Dictionary

Values stored in a dictionary can be accessed using their keys.

For example:

print(user["name"])
print(user["age"])

Here the key is used to retrieve the associated value.

Unlike lists and tuples, dictionaries do not rely on numeric positions. Instead, each value is accessed through its corresponding key.

This approach makes dictionaries ideal for representing structured data.

Adding and Updating Data

Dictionaries are mutable, which means their contents can be modified after creation.

For example, a new key–value pair can be added like this:

user["email"] = "mohan@example.com"

If the key already exists, assigning a new value updates the existing entry:

user["age"] = 22

These operations allow programs to update and expand structured data as new information becomes available.

Removing Elements From a Dictionary

Entries can also be removed from dictionaries when they are no longer needed.

For example:

del user["role"]

Python also provides methods such as pop() for removing elements and retrieving their values.

age = user.pop("age")

These operations help programs manage dynamic data structures.

Iterating Through Dictionaries

Dictionaries can be processed using loops, allowing programs to examine each key–value pair.

For example:

for key in user:
    print(key, user[key])

Python also provides methods such as items(), keys(), and values() to iterate through different parts of a dictionary.

for key, value in user.items():
    print(key, value)

This allows programs to process structured data efficiently.

Dictionary Methods and Built-in Operations

Python provides several useful methods for working with dictionaries.

Some commonly used operations include retrieving keys, values, and key–value pairs, checking whether a key exists, and merging dictionaries.

For example:

print(user.keys())
print(user.values())

These operations allow programs to examine and manipulate dictionary data easily.

Dictionaries in Real Programs

Dictionaries appear everywhere in real software systems because they represent relationships between pieces of data.

  • Web applications use dictionaries to store request parameters and configuration settings.
  • Data-processing programs use dictionaries to represent structured records.
  • APIs often exchange information using dictionary-like structures such as JSON.

For example, a user profile returned from an API may look like this:

{
    "id": 101,
    "username": "mohan",
    "email": "mohan@example.com"
}

This format allows programs to retrieve specific values using descriptive keys.

Why Dictionaries Matter

Dictionaries provide one of the most powerful and flexible ways to organize data in Python. By mapping keys to values, they allow programs to represent complex relationships clearly. They make it easy to store structured information, update records, and retrieve specific values quickly.

Because many real-world systems rely on key–value relationships, dictionaries are one of the most important data structures to understand in Python.

What Comes Next

Now that you have explored the major built-in data structures in Python — lists, tuples, sets, and dictionaries — the next step is learning how to work with them more effectively.

Python provides many built-in functions that simplify common operations on collections, such as counting elements, sorting data, finding minimum or maximum values, and transforming sequences.

In the next article, you will explore Built-in Functions for Data Structures, where you will learn how functions like len(), sum(), min(), max(), sorted(), and others help process collections efficiently. Because once a program can store structured data, the next challenge is analyzing and manipulating that data in powerful ways.

Python Dictionaries | Learn Syntax | Learn Syntax