15
Lesson 8 of 44

Dictionaries

Dictionaries are the most widely used data structure in real-world Python: they store information by associating a key with a value. In this lesson you will learn how to create, query and modify them, how to loop over them, how to count frequencies and group data, and how to work with nested dictionaries and lists of records. Reinforce your skills with practical exercises such as address books, inventories and word counts.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

What is a dictionary

A dictionary ( dict ) stores information as key: value pairs. Instead of accessing the data by its position, as in a list, you…

Theory
Read more

Looping over a dictionary

There are three ways to loop over a dictionary depending on what you need: only the keys, only the values, or both at once.

Theory
Read more

Counting, grouping and inverting

Many real problems are solved with three dictionary patterns: counting how many times something appears, grouping elements by a…

Theory
Read more

Nested dictionaries and lists of dictionaries

The values of a dictionary can be anything, including lists and other dictionaries. This lets you represent structured data, such…

Theory
Read more

Common mistakes and good practices

These are the most frequent mistakes when working with dictionaries and how to avoid them.

Theory
Read more

Practice exercises 10

Contact book

Create a Python program that stores contacts in a dictionary and looks up the phone number of one of them.

Exercise Easy
Solve it

Count words

Create a Python program that counts how many times each word of a text appears using a dictionary.

Exercise Easy
Solve it

Letter frequency

Create a Python program that counts how many times each letter of a text appears.

Exercise Easy
Solve it

Most frequent character

Create a Python program that finds the character that appears most often in a word.

Exercise Medium
Solve it

Coffee shop

Create a Python program that calculates the bill of a coffee shop using a dictionary of prices.

Exercise Easy
Solve it

Average per student

Create a Python program that calculates the average grade of each student when the same student can have several grades.

Exercise Medium
Solve it

Reverse translator

Create a Python program that inverts a dictionary of translations so you can look up in the other direction.

Exercise Medium
Solve it

Group by length

Create a Python program that groups the words of a sentence by their length.

Exercise Medium
Solve it

Student records

Create a Python program that stores students as dictionaries inside a list and analyzes them.

Exercise Medium
Solve it

Number to text

Create a Python program that writes each digit of a number in words, using a dictionary.

Exercise Medium
Solve it