03.01 · Find and sort items / Watch · 12 min
Find the first matching item safely
Index zero is a real match. A missing item raises an error instead of returning a position.
Activity 1 of 4 in Find and sort items
Python 3: Lists / 03.01
Find items by value
Not started
Use membership, index and a specific exception handler to handle missing values.
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.

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.
Open video & code editor (opens in a new tab)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
YOUR TURN
Handle each search outcome
Find Pixel; guard purple with membership; handle missing teal with except ValueError. Preserve both input lists and print the three requested results.
3 4 teal not found
Keep the starting inputs. Print these lines in this order, without extra output.
- Keep the requested movie position
- Keep the requested purple position
- Keep the requested purple message
- Keep the requested teal message
- Keep the requested movies
- Keep the requested colors
- Output 1 matches the requested result
- Output 2 matches the requested result
- Output 3 matches the requested result
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Search the list by value
movies.index("Pixel") returns a number. Keep the actual movie list and search value rather than hardcoding that number.
Give missing colors a deliberate path
Check "purple" in colors before index. Catch ValueError around the teal index call. Convert a found numeric position with str when storing the message.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
The found positions are zero-based. A specific missing-value handler lets the final print statements run even when teal has no index.
Walk through the fix.
Choose Next to inspect one change at a time. Highlighted lines show what to notice. Your editor stays unchanged unless you choose to load a step.
Step 1 of 2
Keep the starting collection
Read the actual values and their positions before calculating an answer.
movies = ["Orbit", "Spark", "Loop", "Pixel", "Byte"]Expected output for this step
(No output)
Trace the operation and its result
The found positions are zero-based. A specific missing-value handler lets the final print statements run even when teal has no index.
movies = ["Orbit", "Spark", "Loop", "Pixel", "Byte"]
colors = ["red", "blue", "green", "yellow", "purple", "orange"]
movie_position = movies.index("Pixel")
if "purple" in colors:
purple_position = colors.index("purple")
purple_message = str(purple_position)
else:
purple_position = -1
purple_message = "purple not found"
try:
teal_message = str(colors.index("teal"))
except ValueError:
teal_message = "teal not found"
print(movie_position)
print(purple_message)
print(teal_message)Expected output for this step
3 4 teal not found
See the complete solution
movies = ["Orbit", "Spark", "Loop", "Pixel", "Byte"]
colors = ["red", "blue", "green", "yellow", "purple", "orange"]
movie_position = movies.index("Pixel")
if "purple" in colors:
purple_position = colors.index("purple")
purple_message = str(purple_position)
else:
purple_position = -1
purple_message = "purple not found"
try:
teal_message = str(colors.index("teal"))
except ValueError:
teal_message = "teal not found"
print(movie_position)
print(purple_message)
print(teal_message)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)