0Pricing
Learn AI with Python · Lesson

Dictionaries

Master the use of key-value pairs to store and retrieve data efficiently.

Dictionaries is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Dictionaries

Dictionaries in Python are collections of key-value pairs. They allow you to store and retrieve data efficiently using keys.

In this lesson, you’ll learn how to create, manipulate, and iterate over dictionaries.

Dictionaries — illustration 1

2

Creating Dictionaries

A dictionary is created using curly braces {}, with key-value pairs separated by colons:

# Creating a dictionary
person = {"name": "Alice", "age": 25}
print(person)

3

4

Accessing Values

You can access a value in a dictionary using its key:

# Accessing values in a dictionary
person = {"name": "Alice", "age": 25}
print(person["name"])  # Outputs: Alice

5

Adding and Updating Key-Value Pairs

You can add or update key-value pairs in a dictionary:

# Adding and updating key-value pairs
person = {"name": "Alice"}
person["age"] = 25  # Adding a new key-value pair
person["name"] = "Bob"  # Updating an existing key
print(person)

6

Removing Key-Value Pairs

You can remove items from a dictionary using the del keyword or the pop() method:

# Removing items from a dictionary
person = {"name": "Alice", "age": 25}
del person["age"]
print(person)  # Outputs: {'name': 'Alice'}

7

Iterating Over Dictionaries

You can iterate over keys, values, or key-value pairs in a dictionary:

# Iterating over a dictionary
person = {"name": "Alice", "age": 25}
for key, value in person.items():
    print(f"{key}: {value}")

8

Using Dictionary Methods

Dictionaries come with useful methods: 

  • keys(): Returns all keys. 
  • values(): Returns all values. 
  • items(): Returns key-value pairs.
# Dictionary methods
person = {"name": "Alice", "age": 25}
print(person.keys())  # Outputs: dict_keys(['name', 'age'])

9

10

Common Mistakes with Dictionaries

Here are some mistakes to avoid:

  • Using mutable types (like lists) as keys in a dictionary (raises an error).
  • Trying to access a non-existent key without using get().
  • Overwriting an existing key unintentionally.

11

What Did We Learn?

In this lesson, you learned:

  • How to create and manipulate dictionaries in Python.
  • How to add, update, and remove key-value pairs.
  • How to iterate over dictionaries and use their methods.

Great job! Let’s move to the next topic.

Dictionaries — illustration 11

Frequently asked questions

Is the “Dictionaries” lesson free?

Yes — the full text of “Dictionaries” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Dictionaries”?

Master the use of key-value pairs to store and retrieve data efficiently. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Dictionaries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lists
  2. Tuples
  3. Sets
  4. Dictionaries
  5. Strings as Data Structures
← Back to Learn AI with Python