BugLab009 / 020up to 100
comparisonidentityequalitygotchamedium

Identity Crisis

The function find_in_list(items, target) should return the index of the first element in items that is equal in value to target. It returns -1 if not found.

It works for most cases — but searching for True in a list containing 1, or False in a list containing 0, mysteriously returns -1 even though those values should match.

The comparison line looks perfectly reasonable. What is Python actually checking?

hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def find_in_list(items, target):
"""
Returns the first index where target appears in items.
Returns -1 if not found.
Example: find_in_list([10, 20, 30], 20) -> 1
"""
for i, item in enumerate(items):
if item is target:
return i
return -1
>>> find_in_list([1, 2, 3], True)