Predict the output
What does this print?
from bisect import bisect_left
words = sorted(["mouse", "mobile", "money", "monitor", "mousepad"])
i = bisect_left(words, "mou")
print(words[i:i + 3])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A ['mouse', 'mousepad']
Right: only two words start with 'mou'; a third slot holds nothing.
Slicing past the end of a list is safe in Python, but a real answer must also check each word starts with the prefix.
Why not the others?
- B ['mouse', 'mousepad', 'money']. money sorts before mouse, so it is not after the search position.
- C ['mobile', 'money', 'monitor']. Those come before 'mou' in sorted order.