Lesson 22 of 55
9 mins readPython Dictionaries (Dict) & Key-Value Maps
In Plain English
Dict in Julia stores associative key-value pairs created with the Pair operator (key => value). Learn lookups, get() with fallbacks, and dictionary comprehensions.
Deep Dive: How It Works
Dict Syntax: Dict("a" => 1, "b" => 2) initializes a hash map.
Key Lookup: d["a"] retrieves value; throws KeyError if key does not exist.
Safe Lookup: get(d, "c", 0) returns 0 if key "c" is missing without throwing an error.
Dict Comprehension: Dict(x => x^2 for x in 1:5).
Core Rules to Remember

Pair Operator =>: Construct key => value associations cleanly.

Safe get() with Defaults: Avoid KeyErrors using get(dict, key, default).
Live Interactive Example
Hit Run Code to see it liveDictionary Operations and Iteration
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: Dict stores arbitrary keys and values with O(1) average lookup.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Count Word Frequencies
Create a dictionary `counts = Dict("apple" => 3, "banana" => 5)`.
Add `counts["orange"] = 2`.
Print `"Total apples: $(counts["apple"])"`.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.