This web browser is not supported. Use Chrome, Edge, Safari, or Firefox for best results.

Sample Python Scripts aka Fun with Word Lists

Python is becoming a popular programming language because it's both powerful and easy to learn, and it's great for exploring word lists. Here are some sample scripts to get you started.

These programs output to the console, but you can output to a file by appending "> outfile.txt" to the command line when you run the program.

1. Find words that start and end with the same letter, or same string of letters

Here's a simple script you can use as a template for exploring your own word list, whether you got it from us or you built it yourself.

Once you have a Python environment set up on your computer, you can copy and paste this script, change the "open" statement to point to your text file, and run it to find all the words that start and end with "x" or with the string "de" or whatever.

That's a very specific example, but to find the words you're looking for, all you have to do is replace the test at the end (if word.startswith...) with logic that fits your goal.

Note the test here is case-sensitive. How would you change this script to handle upper- or lower-case equally?

import sys

# Get the search string from command line, or prompt if it's missing.

pattern = sys.argv[1] if len(sys.argv) > 1 else input("Enter Start/End search string: ")

# Bail if we didn't get a search string.

if len(pattern) == 0:
    sys.exit()

# Open the text file in read-only mode.
# If it isn't in current directory, specify full path using forward slash ("/") not backslash.

with open("XwiJeffChenList.txt", "r") as f:
    lines = f.readlines()

i = 0  				# counter to prevent runaway results

for line in lines:

    parts = line.split(';')
    word = parts[0]  		# Get the first part of the line, the word before ";"

    if word.startswith(pattern) and word.endswith(pattern):

        print(word)

        if i > 10000:
            break  		# Limit to first 10000 words

        i += 1

2. Find words that match a RegEx pattern

This script builds on the first one by replacing the simple start/end test with a RegEx match.

You can explore RegEx on our Finder page and see some Regex examples here.

import re
import sys

# Get the RegeEx pattern from the command line parameter. Prompt if missing.

pattern = sys.argv[1] if len(sys.argv) > 1 else input("Enter RegEx: ")

if len(pattern) == 0:
    sys.exit()

# Compile the pattern into a RegEx Object so it only has to be evaluated once.

prog = re.compile(pattern.replace("$v", "[aeiou]").replace("$c", "[^aeiou]"), re.IGNORECASE)

with open("XwiJeffChenList.txt", "r") as f:
    lines = f.readlines()

i = 0

for line in lines:

    parts = line.split(';')
    word = parts[0]

    if prog.search(word):

        print(word)

        if i > 10000:
            break

        i += 1

3. Find words that exist with or without a specified suffix

A 2021 crossword played on the idea of words with or without the suffix ION. This modification to the program finds words with and without any suffix you enter.

# Get the suffix string from command line, or prompt if it's missing.

suffix = sys.argv[1] if len(sys.argv) > 1 else input("Enter suffix: ")

# Bail if we didn't get a search string.

suffixLength = len(suffix)

if suffixLength == 0:
    sys.exit()

# Open the text file in read-only mode.
# If it isn't in current directory, specify full path using forward slash ("/") not backslash.

with open("XwiJeffChenList.txt", "r") as f:
    lines = f.readlines()

i = 0               # counter to prevent runaway results
fullWordList = []   # list of all words (not including scores) from file

for line in lines:

    parts = line.split(';')
    fullWord: str = parts[0]      # Get the first part of the line, the word before ";"
    fullWordList.append(fullWord) # All words get added to list

    # If the word ends with our suffix, see if we have already encountered that word without the suffix.
    # Note, since the list is parsed only once, this program assumes that the list is in alphabetical
    # order. So, for example, PASS would precede PASSION.

    if fullWord.endswith(suffix):

        truncatedWord = fullWord[: -suffixLength]

        if truncatedWord in fullWordList:

            print(truncatedWord + ":" + fullWord)

            if i > 10000:
                break         # Limit to first 10000 words

            i += 1

The rest is up to you, your ingenuity, and your creativity. Have fun!

If you're serious about learning Python, XWord Info recommends PyCharm. The free "Community" version works fine.