Python Dictionaries, Default Lookups & Tuples
Work with associative key-value mappings and ad-hoc compound types. Master Swift Dictionaries ([Key: Value]), subscript lookups with default values, and Tuples ((httpCode: 200, status: "OK")) for lightweight multi-value returns.
"A Dictionary is like an airport flight information board: look up flight "AA100" (key) and instantly see the destination "JFK" (value). A Tuple is like a passport: a small bound booklet holding your name, birth year, and nationality together."
Deep Dive: How It Works
Dictionaries: Hash map storing unique Hashable keys mapped to values ([String: Int]).
Dictionary Subscripts: Subscripting dict[key] returns an Optional Value (Value?) because keys may not exist.
Default Parameter Lookups: dict[key, default: 0] returns a non-optional default value if key is missing.
Tuples: Group multiple values into a single compound type ((Int, String) or named (code: 200, message: "OK")).
Syntax Blueprint
var counts: [String: Int] = ["swift": 100] let count = counts["go", default: 0] let response = (code: 200, status: "OK")
Use [Key: Value] for hash tables with default values, and (name: value) for tuples.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using tuples for complex persistent data models.Why it happens: Tuples lack methods, property observers, and protocol conformance.
How to fix: Use structs for complex entities and reserve tuples for temporary grouped values.
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.
Query Product Price with Default Fallback
Declare let catalog: [String: Double] = ["laptop": 999.0, "mouse": 25.0].
Query the price of "monitor" using default: 0.0.
Print "Price of monitor: $\(price)".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.