Python Dictionaries

Python 10 min min read Updated: Mar 09, 2026 Intermediate
Python Dictionaries
Intermediate Topic 4 of 10

Dictionaries in Python

Dictionaries are one of the most powerful data structures in Python. They allow developers to store data in the form of key-value pairs. Each key is unique and is used to access its corresponding value.

Dictionaries are widely used in real-world applications such as storing user data, configuration settings, JSON responses, and database-like structures.

What is a Dictionary in Python?

A dictionary is an unordered, mutable collection of key-value pairs. Dictionaries are created using curly braces {} where each element consists of a key and its associated value.

python student = { "name": "Alice", "age": 21, "course": "Python" } print(student)

In this example, the dictionary stores three pieces of information about a student.

Accessing Dictionary Values

You can access dictionary values using their keys.

python student = { "name": "Alice", "age": 21, "course": "Python" } print(student["name"]) print(student["age"])

The key acts as an identifier that retrieves the associated value.

Using the get() Method

The get() method allows safe access to dictionary values without causing an error if the key does not exist.

python student = {"name": "Alice", "age": 21} print(student.get("name")) print(student.get("course"))

If the key does not exist, None will be returned.

Adding or Updating Values

Dictionaries are mutable, meaning you can add or modify values.

python student = {"name": "Alice", "age": 21} student["age"] = 22 student["course"] = "Python" print(student)

This updates the age and adds a new key-value pair.

Removing Items from a Dictionary

Python provides several methods to remove items from a dictionary.

Using pop()

python student = {"name": "Alice", "age": 21} student.pop("age") print(student)

Using del

python student = {"name": "Alice", "age": 21} del student["age"] print(student)

Using clear()

python student = {"name": "Alice", "age": 21} student.clear() print(student)

Dictionary Methods

Method Description
keys() Returns all dictionary keys
values() Returns all dictionary values
items() Returns key-value pairs
update() Updates dictionary with another dictionary
pop() Removes specified key
clear() Removes all elements

Looping Through a Dictionary

You can iterate through dictionary elements using loops.

Loop Through Keys

python student = {"name": "Alice", "age": 21, "course": "Python"} for key in student: print(key)

Loop Through Values

python for value in student.values(): print(value)

Loop Through Key-Value Pairs

python for key, value in student.items(): print(key, ":", value)

Nested Dictionaries

Dictionaries can also contain other dictionaries, creating nested structures.

python students = { "student1": {"name": "Alice", "age": 21}, "student2": {"name": "Bob", "age": 22} } print(students["student1"]["name"])

This allows complex structured data to be stored efficiently.

Real-World Example

Dictionaries are commonly used to represent structured data such as user profiles.

python user = { "username": "john123", "email": "john@example.com", "is_active": True } print("Username:", user["username"]) print("Email:", user["email"])

Best Practices for Using Dictionaries

  • Use meaningful keys.
  • Avoid duplicate keys.
  • Use get() when unsure if a key exists.
  • Keep dictionary structures simple and readable.

Conclusion

Dictionaries are a powerful and flexible data structure in Python that allow data to be stored and accessed using key-value pairs. They are widely used in real-world applications for storing structured data such as configurations, user information, and API responses.

By mastering dictionaries and their built-in methods, developers can efficiently organize and manage data in Python programs.

In the next tutorial, we will explore String Handling in Python and learn how to manipulate text data effectively.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators