Process Overview
Search and find operations help us look for a specific item inside a list or collection. This process checks each item one by one until it finds the target or finishes checking all items.
Jump into concepts and practice - no test required
Search and find operations help us look for a specific item inside a list or collection. This process checks each item one by one until it finds the target or finishes checking all items.
List: [3, 7, 1, 9] Index: 0 -> 3 (checked) Index: 1 -> 7 (checked) Index: 2 -> 1 (target found) Index: 3 -> 9 (not checked)
numbers?index() method to find the position of an element.numbers.index(5) is correct syntax to find element 5's index.items = [3, 7, 1, 9, 7] print(items.index(7))
items contains [3, 7, 1, 9, 7]. The index() method returns the first position of the value.data. What is wrong?data = [4, 8, 10, 15] position = data.find(10) print(position)
find() method; they use index() to find element positions.find() with index() fixes the error.students = ['Anna', 'Bob', 'Cara', 'Dan', 'Eli']. You want to check if 'Zoe' is in the list and print her position if found, otherwise print -1. Which code snippet correctly does this efficiently?print(students.index('Zoe') if 'Zoe' in students else -1) uses conditional expression to print index or -1 safely and efficiently. for i in range(len(students)): if students[i] == 'Zoe': print(i) break loops but prints nothing if not found. print(students.find('Zoe')) uses invalid find() for lists. if 'Zoe' in students: print(students.index('Zoe')) prints only if found, nothing otherwise.