Coding/Learn/python/dictionaries
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnDictionaries & Key-Value Stores
24 / 32 0%
Lesson 24 of 32
7 mins read

Dictionaries & Key-Value Stores

PreviousNext Lesson
In Plain English

A dictionary is like a real dictionary or phone book: instead of looking up an item by a number index (like in a list), you look up a value by its key label. Defined with curly braces and key: value pairs: user = {"name": "Alex", "level": 10}.

The Mental Model

"Think of a contact card on your smartphone. The keys are "Name", "Phone", "Email", and the values are the actual contact details. You look up a contact by their name, not by "contact #42"."

Deep Dive: How It Works

Dictionaries store data in key: value pairs separated by commas inside curly braces {}.

Accessing values: Use square brackets with the key name: user["name"], or safely with user.get("name", "Default").

Keys must be unique and immutable (strings and integers make great keys). Values can be anything, including lists and nested dictionaries.

Modifying dictionaries: Add or update keys simply by assigning: user["level"] = 11. Delete keys with del user["key"] or user.pop("key").

Syntax Blueprint

user = {"name": "Alex", "role": "Admin"}
name = user["name"]       # Access by key ("Alex")
user["xp"] = 500          # Add new key
role = user.get("role")   # Safe access

Keys and values are paired with colons : and separated by commas.

PointCore Rules to Remember

Point star
Key-Value Architecture: Store named fields like name: "Alex", score: 95.
Point star
Fast Key Lookups: Looking up a key in a dictionary is instant (O(1) hash table).
Point star
.get() for Safe Lookups: Avoid KeyError crashes by using .get(key, fallback) when keys might be missing.

Common Beginner Traps & How to Fix Them

Mistake:KeyError when accessing a non-existent key: user["email"]

Why it happens: If the key does not exist in the dictionary, bracket notation crashes with a KeyError.

How to fix: Use user.get("email", "Not provided") to return a safe fallback value instead of crashing.

Pro Tip: Iterate over both keys and values together using .items(): for key, value in user.items():.

Live Interactive Example

Hit Run Code to see it live
Managing User Profile in a Dictionary
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: We read the username, incremented credits in place, and used .get() to handle missing email safely.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a dictionary and access a key

  • PointCreate a dictionary server with keys "host": "localhost" and "port": 8080.
  • PointPrint the value of server["port"].
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates