YOUR SAVED CODE-ALONG
Code along: Find items by value
Keep the lesson open on your other screen. Your edits stay in this workspace, separate from the practice activity. Sign in to save and reload a private account draft across devices, or share a snapshot when you need help.
CODE-ALONG / THEATER MODE
Find items by value
ORIGINAL WALKTHROUGH · 5:05
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Video settings & progress
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.
Start the video to enable typing controls.
YOUR TURN
Code along: Find items by value
Find the first matching position, including index zero. Compare a membership guard with catching ValueError. Try an existing item and a missing item. Pause the original recording to type and run your examples. This open workspace saves separately from scored practice and has no fixed-answer checks.
Load an example, edit it and run it. Word wrap is enabled.
Your check results
Lesson reference
Examples load into this same code-along only after you confirm replacing your code.
index returns a position, not the item
colors.index("red") returns the zero-based position of the first equal value. A duplicate later in the list does not change that first result. Position 0 is a valid answer; do not use if position to decide whether an item was found.
Unlike some other languages, Python list.index does not return -1 for a missing item. It raises ValueError.
colors = ["red", "blue", "red", "green"]
position = colors.index("red")
print(position) # 0
print(colors[position]) # redCheck membership before asking for an index
Use value in colors to ask whether a matching item exists. Call index only in the branch where that membership test is true. The else branch explains that no matching item exists.
The list is unchanged throughout this search. Membership answers True or False; index answers a number.
colors = ["red", "blue", "green"]
wanted = "purple"
if wanted in colors:
print(colors.index(wanted))
else:
print("Color not found")Catch the specific missing-value error
try runs its indented statements. If colors.index raises ValueError, Python runs the matching except block instead. Code after the block can continue.
Catch ValueError for this known situation rather than hiding every error with a bare except. A membership check and a specific exception handler are two options; neither is universally better in all programs.
colors = ["red", "blue", "green"]
try:
position = colors.index("teal")
print(position)
except ValueError:
print("teal not found")
print("Search complete")Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)4 steps · Saved work · Instant feedback
See the attached lesson’s comic
