π§ Start Here
Scroll down to complete this route
The Gear Rack: Lists, Dicts, and Loops
- RouteID: 003
- Wall: Getting Comfortable with Python
- Routesetter: Adrian
- Grade: 5.7
- Time: ~35-60 minutes
- You'll need: A Jupyter/Colab notebook (or any place you can run Python cells)
Why this route exists
In applied AI/ML for biochemistry, you constantly work with collections of things:
- a list of protein sequences you want to score or cluster
- a list of metabolites detected in an LC-MS run
- a list of genes that pop out of a CRISPRi screen
- a dict mapping
gene β log2FCorprotein_id β embedding_vector - a dict mapping
compound β intensityorsample β metadata
In Python, containers (lists + dicts) hold those collections, and for-loops let you walk through them to do real work:
- Filter β
keep only hits with |log2FC| > 1 - Count β
how many were significant? - Summarize β
top 10 by intensity - Transform β
build a new dict keyed by gene name
This route is your βgear rackβ: youβll practice packing biological data into containers, then looping through it to generate clean, trustworthy summaries.
What you'll be able to do after this route
By the end, you can:
- Create and edit a list
- Create and edit a dict
- Use
forloops to iterate over:- list items
- list indices (with
range) - dict keys and dict key/value pairs (with
.items())
- Build new containers from old ones (filtered lists, count dictionaries)
What this route is NOT about (on purpose)
whileloops- list comprehensions (we'll do "plain loops" first)
- pandas / DataFrames
- performance tricks
Route rules (common "falls" to avoid)
- Don't name variables
list,dict, orrange(those are built-in names). - Don't write
for gear in gear:(don't reuse the container name as the loop variable). - Don't remove items from a list while you're looping over it. Make a new list instead.
- If you get
KeyError, use.get()or checkif key in my_dict.
Pitch 1 β Lists: Packing the rack
Setup (copy this into one cell and run it)
# A list: ordered items (can contain duplicates)
gear = ["ATC", "chalk", "helmet", "quickdraw", "quickdraw", "locking carabiner"]
Exercise 1 β Inspect your rack (list basics)
Goal: Check what you have and add one item. Do:
- Print the entire
gearlist. - Print the number of items in
gear. - Print the first item and the last item.
- Add
"tape"to the list. - Print
gearagain to confirm it changed.
Helpful hints (not required):
len(gear)gear[0]gear[-1]gear.append("tape")
Success check: You see "tape" at the end, and your length increased by 1.
Exercise 2 β Inventory walk (your first for-loop)
Goal: Print each item in your rack on its own line. Do:
- Write a
forloop that prints each item ingear.
Success check:
You see one line per item, including duplicates like "quickdraw" twice.
Common fall:
If you wrote for gear in gear:, rename the loop variable:
for item in gear:
print(item)
Exercise 3 β Inventory with positions (indices)
Goal: Print index -> item for your gear list.
Example output (yours can differ):
0 -> ATC
1 -> chalk
2 -> helmet
...
Do ONE of the two options: Option A (recommended):
- Use
enumerate(gear).
Option B:
- Use
range(len(gear))and index into the list.
Success check: Your output includes both the index number and the item.
Pitch 2 β Filtering: Sorting gear into bins
Exercise 4 β Build a filtered list (don't mutate!)
Goal: Make a new list called carabiners that contains only gear items with "carabiner" in the name.
Do:
- Create an empty list:
carabiners = [] - Loop over
gear - If the item contains
"carabiner", append it tocarabiners - Print
carabiners
Helpful hint:
"carabiner" in item
Success check:
carabiners should contain "locking carabiner".
Common fall:
Don't do gear.remove(...) inside your loop. That's a classic foot slip.
Pitch 3 β Dicts: The inventory sheet
Setup (copy this into one cell and run it)
# A dict: name --> count (keys are unique)
gear_counts = {"quickdraw": 6, "locking carabiner": 3, "helmet": 1}
Mini-intro: What is a dictionary?
A dictionary (usually called a dict) is a container that stores key β value pairs. Think of it like a labeled box system:
- key = the label (usually a string like
"Rv0281"or"quickdraw") - value = the thing you want to store (a number, string, list, etc.)
Why biochemists love dicts:
gene β log2FC(CRISPRi/TnSeq hits)protein_id β sequencecompound β intensitysample_id β metadata
Two rules to remember
- Keys are unique: If you assign the same key twice, the later value overwrites the earlier one.
- You look up values by key:
d["helmet"]gets the value for"helmet".
Exercise 5 β Dict basics + avoiding KeyError
Goal: Safely look up counts of items. Do:
- Print the value for
"helmet"fromgear_counts - Try to look up
"ATC"(it may not exist ingear_counts) - Fix the lookup so it does not crash when the key is missing
Helpful hints:
gear_counts["helmet"]gear_counts.get("ATC", 0)
Success check: You can request counts for both present and missing items without an error.
Exercise 6 β Count items from a list (frequency dict)
Goal: Build a new dict counts_from_list from gear.
You want output like:
{"ATC": 1, "chalk": 1, "helmet": 1, "quickdraw": 2, "locking carabiner": 1}
Do:
- Create an empty dict:
counts_from_list = {} - Loop over
gear - For each item, add 1 to its count
Two valid patterns (pick one): Pattern A (explicit):
- If the item is not a key yet, start at 0
- Then increment
Pattern B (compact):
counts_from_list[item] = counts_from_list.get(item, 0) + 1
Success check:
counts_from_list["quickdraw"] is 2.
Exercise 7 β Looping over dicts (clean report)
Goal: Print a clean inventory report from counts_from_list.
Example:
ATC: 1 chalk: 1 helmet: 1 quickdraw: 2 locking carabiner: 1
Do:
- Loop over
counts_from_list.items()and printkey: value
Success check: Every key/value prints once.
Pitch 4 β Real-ish data: A list of dict "records"
Setup (copy this into one cell and run it)
# A list of dicts: each attempt is a little record
attempts = [
{"grade": "5.9", "tries": 2, "sent": True},
{"grade": "5.10a", "tries": 4, "sent": False},
{"grade": "5.10b", "tries": 3, "sent": True},
]
Exercise 8 β Summarize attempts
Goal: Compute:
- Total number of tries across all attempts
- A list of grades you sent (
sent == True)
Do:
- Start with:
total_tries = 0sent_grades = []
- Loop through
attempts - Add
attempt["tries"]tototal_tries - If
attempt["sent"]is True, appendattempt["grade"]tosent_grades
Success check:
total_triesis the sum of thetriesvaluessent_gradescontains"5.9"and"5.10b"(the routes where sent is True)
Common falls:
- If you get
KeyError, print the attempt dict and check the key spelling. - If your totals look wrong, print inside the loop:
- the current
tries - the running
total_tries
- the current
Anchor Challenge β Rack Check (no hints)
Goal: Build a dict mapping grade -> tries using attempts.
Example:
{"5.9": 2, "5.10a": 4, "5.10b": 3}
Then print only the routes that took 3 or more tries. Rules:
- Use a loop (no pandas).
- It's fine if you print in any format you like.
Success check:
- Your dict has one entry per route
- Your printed output includes
"5.10a"and"5.10b"(in this dataset) because they have 3+ tries
Optional "Bonus Hold" (only if you're cruising)
Rewrite Exercise 4 (filtered list) using a list comprehension. Example pattern: new_list = [item for item in gear if "carabiner" in item]
No pressure β the route is complete without this.
Deliverables
Please submit the following two items:
1. A completed Jupyter notebook (.ipynb)
- The notebook should run top-to-bottom without errors.
- It should include your code and any brief comments you added while working.
- Please follow this file naming convention β lastname_firstname_RID_003_code.ipynb
- The RID stands for "Route ID". This would be route #003.
How to download from Google Colab:
- In Colab, click File β Download β Download .ipynb
- This will save the notebook to your computer.
2. A short logbook entry (plain text, ~5-10 sentences):
- Briefly describe:
- what was tricky or confusing
- what helped you get unstuck
- one thing you learned about working with real data
- File naming convention β lastname_firstname_RID_003_logbook.txt
- Focus on clarity and completeness.
Submission
Submit your files by uploading them to this Google form: SUBMIT LINK
Please upload both:
- your .ipynb notebook
- your logbook file
Make sure filenames follow the naming conventions above.
We will fine-tune our submission system as the course moves along. Thank you for your patience as a valued member of the CHEM 169/269 Climbing Gym.
π Route Complete!
Great work!