Skip to content

Instantly share code, notes, and snippets.

@pythonhacker
Last active January 6, 2026 14:10
Show Gist options
  • Select an option

  • Save pythonhacker/757dac70b09e79664596519f810fef34 to your computer and use it in GitHub Desktop.

Select an option

Save pythonhacker/757dac70b09e79664596519f810fef34 to your computer and use it in GitHub Desktop.
Word search using prefix search with the bisect module
def search_word(filename, prefix):
""" Search a file containing a wordlist and print matching words by prefix """
wordlist = sorted(filter(None, [item.lower().strip() for item in open(filename)]))
words = []
idx = bisect.bisect_left(wordlist, prefix)
for idx in range(idx, len(wordlist)):
word = wordlist[idx]
if word.startswith(prefix):
words.append(word)
return words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment