Python · live now

Dictionaries

Python's mapping from keys to values: looking a value up, testing membership, adding and removing entries, iterating over keys and values, insertion order, and merging two dictionaries.

No account needed to read a lesson · 30-day money-back guarantee

See the full learning path

8

lessons

65

practice questions

45

prerequisite lessons

See it taught

1 of the 8 lessons here is free to preview - the whole explanation and worked example, with no account and no email.

The editor you get

A still of the editor, not a live one

Python Editor

1 2 3
ages = {"ada": 36, "alan": 41} ages["grace"] = 45 print(ages.get("ada"), len(ages))

Editor Output

1

Output:

36 3

You write and run Python in the browser - nothing to install, no setup. Questions are marked on the values your code produces. The snippet here is an example of the kind of code you write.

The 8 lessons

The 8 lessons in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.

01

Dictionaries

12 questions

Learn how Python dictionaries store data as mutable key-value pairs, see how to create dictionaries with literal values, and use the len() function to determine their size.

Covers

Dictionaries overview

Creating a dictionary using literal values

Length of a dictionary

02

Accessing values in a dictionary

8 questions

Learn how to retrieve values from a Python dictionary using both square bracket notation and the .get() method, including how to handle missing keys without causing errors.

Covers

Dictionary lookup with []

Dictionary lookup with .get()

03

Dictionary membership

4 questions

Learn how to use the in and not in keywords to check whether a given key exists in a Python dictionary.

Covers

Dictionary membership

04

Modifying a dictionary

Preview

16 questions

Learn how to update, add, and delete dictionary entries in Python using assignment and the del statement, as well as the rules that determine which objects can be used as keys.

Covers

Updating a dictionary entry

Adding a new entry to a dictionary

Removing an entry from a dictionary

Dictionary keys

Preview this lesson - no account needed

05

Iterating over a dictionary

12 questions

Learn how to iterate over a dictionary's keys, values, or key-value pairs in Python using built-in methods to access and process the information efficiently.

Covers

Iterating over a dictionary's keys

Iterating over a dictionary's values

Iterating over a dictionary's keys and values

06

Dictionary insertion order

4 questions

Python dictionaries preserve the order in which items are inserted, so iterating over a dictionary yields key-value pairs in insertion order rather than in an arbitrary sequence.

Covers

Dictionary insertion order

07

Merging two dictionaries

4 questions

Learn how to merge the key-value pairs of two dictionaries in Python using the .update() method, which adds new keys and overwrites existing ones in place.

Covers

Merging two dictionaries

08

Activity: library system

5 questions

Practise implementing a simple Python-based library management system by writing code to check book availability, add new books, lend books, list available books, and merge library collections.

Covers

Check book availability

Adding new books

Lending a book

Available books

Merging library collections

Where this unit sits in the curriculum

This unit is part of our Python Done Right learning path, which contains 100 lessons. Every one of them is drawn below.

The diagnostic test lets you skip any lesson you already know, including the ones in this unit.

Every dot is a lesson and every line a prerequisite. The 8 red dots are the lessons in Dictionaries; the 45 blue dots feeding into them are the lessons Dictionaries depends on; the 47 pale dots are the rest of the learning path, which come after Dictionaries or alongside it.DictionariesFirst lessons on the left100 lessons

Hover any dot to name its lesson.

8 lessons in this unit

45 prerequisite lessons across the unit, counting every step back to the start

47 other lessons in the learning path - after this unit, or alongside it

Every dot is a lesson, every line a prerequisite. You can only start a lesson once you have mastered all of its prerequisites, so you are always building on solid foundations.

Start Dictionaries

One subscription covers every learning path, and you can test out of anything you already know.

One subscription covers every learning path · 30-day money-back guarantee

More in Data structures: Lists, Tuples, Sets