Skip to content
IT Vizag
← Lists, Dicts & Data

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

Dictionaries

Lesson 5 of 9 · 1 min read

Look things up by key: phone books, prices and settings.

🎬 Video version coming soon — read it now

A dictionary (dict) stores key → value pairs. Instead of looking things up by position, you look them up by name, like a phone book.

🔒 Code example: sign up free (above) to unlock it.

Adding, changing and removing

🔒 Code example: sign up free (above) to unlock it.

Keys must be unique. Setting a key that already exists replaces its value.

Safe lookups

Asking for a key that doesn't exist gives a KeyError:

🔒 Code example: sign up free (above) to unlock it.

Looping over a dictionary

🔒 Code example: sign up free (above) to unlock it.

Counting with a dictionary

A classic task: how often does each word appear?

🔒 Code example: sign up free (above) to unlock it.

counts.get(w, 0) + 1 means "the current count, or 0 if it's new, plus one". You'll use this pattern constantly.

Values can be anything

🔒 Code example: sign up free (above) to unlock it.
Tip: Keys are usually strings or numbers. Tuples work too, like {(17.69, 83.22): "Vizag"}, but lists don't, because keys must never change.

In one line

A dictionary looks values up by key; use .get() for safe lookups and .items() to loop over pairs.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend