Tutorial 11/15 DATA STRUCTURES

Python: Linear Search

Finding data in a list by checking one-by-one.

The Hook

Finding a song in a playlist.

You scroll down, checking each title one-by-one until you find it.

This is Linear Search. Start at index 0, check, move, check, move...

Worked Example

Finding Nemo in a fish tank.

fish = ["Dory", "Nemo", "Bruce"]
for f in fish:
if f == "Nemo":
print("Found him!")
Logic Gate

Predict Output

What happens to the loop when it finds a match?

nums = [10, 20, 30, 40]
for x in nums:
  if x > 25:
    print(x)
    break
Level 28: Bronze

Golden Ticket

"Search for 'Gold' in the list. If found, print 'Winner!'."

> bronze_stream_standby
Level 29: Silver

Waldo Filter

"Stop the noise! Remove the else block so it only prints when Waldo is found."

> debugger_module_monitoring
Level 30: Gold

Admin Counter

Independent Search Pattern

  • Target List: ["Admin", "Guest", "Root", "Admin"].
  • Initialize count = 0.
  • Loop and increment count whenever "Admin" is found.
  • Print results (Expected: 2).
> computation_vector_online