Lesson 28 of 55
8 mins readPython Hash Slices (@hash{keys})
In Plain English
Like array slices, hash slices extract or assign multiple values in one step using the `@` sigil with curly braces: `@user{"name", "email"}`.
Deep Dive: How It Works
Hash Slice Sigil: `@hash{key1, key2}` uses `@` because a list of values is returned.
Batch Assignment: `@config{"host", "port"} = ("localhost", 8080);`.
Merging: Fast population of hashes from parallel key/value arrays.
Core Rules to Remember

@hash{...} Slice: Uses `@` for list context with curly braces `{}` for hash keys.

Bulk Extraction: Extracts specific subset fields cleanly in one expression.
Live Interactive Example
Hit Run Code to see it liveExtracting and Updating Hash Slices
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Output Console
Click "Run Code" to view the rendered output.
How it works: Hash slice @profile{"name", "city"} extracted and updated fields in bulk.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Extract Metrics with Hash Slice
Declare `%metrics = ( cpu => 45, memory => 70, disk => 30 );`
Extract `@metrics{"cpu", "memory"}` into `my ($c, $m)`.
Print `"Usage: CPU=$c%, MEM=$m%\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.