Python Maps: Key-Value Hash Tables & Safe Access
Maps provide O(1) key-value lookup. Learn initialization with make(), safe existence checking with the comma-ok idiom, and why map iteration order is deliberately randomized in Go.
"A map is like a hotel mail cubby wall. Each room number (key) has a slot containing guest messages (value). Looking up room 404 is instantaneous."
Deep Dive: How It Works
Hash Table Architecture: Implemented as hash buckets holding key-value pairs with O(1) average lookup.
The Comma-Ok Idiom: val, ok := m[key] distinguishes between a missing key and a key with zero value.
Uninitialized Nil Maps: Reading a nil map returns zero value, but writing to a nil map triggers a runtime panic.
Syntax Blueprint
m := make(map[string]int) m["zen"] = 100 val, ok := m["zen"] delete(m, "zen")
Create map with make, assign key-values, inspect existence with comma-ok, delete keys with delete().
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Writing to an uninitialized nil map: var m map[string]int; m["key"] = 1.Why it happens: A nil map header has no allocated hash bucket table.
How to fix: Initialize with make(map[string]int) or a map literal map[string]int{}.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Count Character Frequencies
Write a function charCounts(text string) map[rune]int.
Iterate over text runes and tally their occurrences in a map.
In main(), call charCounts("golang") and print the count of rune 'g'.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.