Lesson 6 - Python Dictionary: A Beginner’s Guide with Examples

Dictionaries in Python are one of the most powerful and commonly used data structures. If you're new to Python, understanding how dictionaries work is a must. In this post, we’ll explain what a Python dictionary is, how to create one, and how to access, update, and delete values — all with clear, easy-to-follow examples.

PYTHON

Leonardo Gomes Guidolin

4/7/20251 min read

What Is a Dictionary in Python?

A dictionary in Python is a collection of key-value pairs, where each key maps to a value.

It's defined using curly braces {} like this:

student = {

"name": "Alice",

"age": 20,

"grade": "A"

}

Key point: Keys must be unique and immutable (like strings or numbers).

How to Create a Dictionary in Python

There are several ways to create a dictionary.

Using Curly Braces

car = {

"brand": "Toyota",

"model": "Corolla",

"year": 2022

}

Using the dict() constructor

car = dict(brand="Toyota", model="Corolla", year=2022)

Accessing Dictionary Values

You can access a value by referencing its key:

print(car["model"]) # Output: Corolla

Or safely with .get():

print(car.get("color", "Not specified")) # Output: Not specified

Adding or Updating Items

To add a new key or update an existing one:

car["color"] = "Red"

Now the dictionary becomes:

{'brand': 'Toyota', 'model': 'Corolla', 'year': 2022, 'color': 'Red'}

Deleting Items from a Dictionary

You can delete keys using the del statement:

del car["year"]

Or with .pop():

model = car.pop("model")

Looping Through a Dictionary

You can iterate through keys and values easily:

for key, value in car.items():

print(f"{key}: {value}")

Output:

brand: Toyota

color: Red

Dictionary Methods You Should Know

.keys() – returns all keys

.values() – returns all values

.items() – returns key-value pairs

.update() – merges another dictionary

.clear() – removes all items

When to Use Dictionaries in Python?

Use dictionaries when you want to:

  • Store data with labels (e.g., user profiles, configurations)

  • Create fast lookup table

  • Group related data using a flexible format

Conclusion

Now you know how to create, access, and manipulate dictionaries in Python. They are a flexible and efficient way to work with structured data, and mastering them will help you build better Python applications.

Keep practicing and check out other Python tips on codeforBeginners.blog!