Dictionaries
Like this video? Get new free AI lessons on YouTube.
SubscribeA dictionary stores data as key-value pairs. Instead of position numbers, you look things up by a meaningful key, like a name or an id. Dictionaries are everywhere: user profiles, settings, counts, and JSON data all use this shape.
What You'll Learn
- Creating dictionaries and reading values
- Adding, updating, and removing keys
- Looping over keys and values
- Safe lookups with
get()
Creating and Reading
Adding and Updating
Assigning to a key that does not exist adds it; assigning to an existing key updates it:
Safe Lookups with get()
Reading a missing key with [ ] raises an error. get() returns None (or a default you choose) instead:
Looping Over a Dictionary
.items() gives you each key and value together:
Nested Dictionaries
A value can itself be a dictionary, which lets you model richer records, like a contact book:
Key Takeaways
- Dictionaries use
{key: value}and look up by key - Assign to add or update;
delto remove get()avoids errors on missing keys.items()loops over keys and values together- Values can be dictionaries, for nested records
Quiz
Up next · Lesson 20 of 24
Sets
1 min read
19 of 24 lessons done

Ask Byte about this lesson
A private chat about this lesson. Byte has read the whole lesson, answers in seconds, and shows the section each answer comes from. Ask follow-ups in any language.
Questions & Answers
Public questions about this lesson. The AI Tutor answers right away in your language, the FreeAcademy team checks answers, and every learner can read them.

