Mapping Phone Numbers to Words in 8 Lines of Python

As a recent convert to Google Fi, I found myself in a position to find a memory tool for my new phone number.  In the interest of speed and laziness, I threw together a script which would produce all the combinations of letters that could be made from my number and dump them to a text file.  It seemed in good taste to share that script.

import itertools
digits = ['0', '1', '2ABC', '3DEF', '4GHI', '5JKL', '6MNO', '7PQRS', '8TUV', '9WXYZ']
digit_map = {k:v for k,v in zip("0123456789", digits)}
phone = "8005551234" # Your number here.
options = [digit_map[n] for n in phone]
with open("words.txt", 'wt') as fout:
  for p in itertools.product(*options):
    fout.write("".join(p) + "\n")

This will yield a text file with a list of entries like (for the above number):


It’s worth noting that we could do away with the digit_map by just indexing directly into the digits, followed by a reverse before the output, but this solution provides greater clarity at a meager cost in efficiency.

Comments are closed.