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

Sample Python Scripts, a.k.a. 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.

Scripting is powerful and can often do searches RegEx can't, but remember, scripts can only find results from your word list. Searches with our Finder also include external dictionaries.

Click any yellow box below to copy that script to your clipboard.

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.

import sys

# This example uses a try/except pattern to handle errors.
# It's a common pattern in Python, and is the preferred way to handle errors.
# The "except" block is only executed if an exception is raised in the "try" block.
# Use this pattern in the other examples here if you like.

class ExitWithMessage(Exception):
    pass

try:
    # Get the search string from command line, or prompt if it's missing.
    # Strip off any leading or trailing spaces and convert to lowercase.
    
    pattern = sys.argv[1] if len(sys.argv) > 1 else input("Enter Start/End search string: ")
    pattern = pattern.strip().lower()
    
    # Bail if we didn't get a search string.

    if len(pattern) == 0:
        print("No search string specified")
        raise ExitWithMessage()
    
    # Open your word list file in read-only mode.
    # If it isn't in current directory, specify full path using forward slash ("/") not backslash.

    with open("XwiWordList.txt") as f:
        for line in f:
            parts = line.split(';') # Split the line into parts, using ";" as the delimiter
            word = parts[0].strip() # Get the first part of the line, the word before ";"
            score = int(parts[1])   # Score isn't used here but this is how you get it
            
            if word.startswith(pattern) and word.endswith(pattern):
                print(word)

except ExitWithMessage:
    sys.exit()

2. 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.

import sys

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

if suffix_length == 0:
    sys.exit()

count = 0
full_word_set = set()   # Set automatically eliminates duplicates and is faster than list

with open("XwiWordList.txt") as f:
    for line in f:
        
        parts = line.split(';')
        full_word = parts[0].strip().lower()
        full_word_set.add(full_word)  # All words get added to set

        # if the word ends with our suffix, see if the word without the suffix is already in the set.
        # Note, this assumes words in alphabetical order, eg, PASS would precede PASSION in the list.

        if full_word.endswith(suffix):
            truncated_word = full_word[:-suffix_length]

            if truncated_word in full_word_set:
                print(f"{truncated_word} : {full_word}")
                count += 1

print(f'{count} found')

3. Words made by combining other words

A 2022 crossword combined 4-letter words to build up 16-letter theme answers. This program searches a word list for all such possible answers. Variable types are declared. That's not necessary for these short programs, but it's good practice.

from typing import List, Set

count: int = 0          # number of matches found
Set4: Set[str] = set()  # Set of all the 4-letter words for fast lookup
List16: List[str] = []  # collection of all the 16-letter words

with open("XwiWordList.txt") as f:
    for line in f:
        word: str = line.split(';')[0].strip().lower()

        if len(word) == 4:
            Set4.add(word)

        if len(word) == 16:
            List16.append(word)

for word16 in List16:
    parts = [word16[i:i+4] for i in range(0, 16, 4)]

    if all(part in Set4 for part in parts):
        print(f'{parts[0]}-{parts[1]}-{parts[2]}-{parts[3]} = {word16}')
        count += 1

print(f'{count} found')

4. Find words that match a RegEx pattern

This script builds on the first sample 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, sys
from typing import Pattern

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

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

# Compile the pattern into a RegEx Object, so it only has to be evaluated once.
prog: Pattern[str] = re.compile(pattern.replace("$v", "[aeiou]").replace("$c", "[^aeiou]"), re.IGNORECASE)

count: int = 0

with open("XwiJeffChenList.txt") as f:
    for line in f:
        word: str = line.split(';')[0].strip().lower()

        if prog.search(word):
            print(word)
            count += 1

print(f'{count} found')

5. Select words that don't contain any of the letters you specify

Specify, say, "aiouy" to show words where the only vowel (if any) is E.

Compare with these Finder results: *-AIOUY using OneLook, and ^[^AIOUY]+$ using Regex.

How would you change the script to select words that only contain letters you specify?

import sys

eliminators = sys.argv[1] if len(sys.argv) > 1 else input("Enter letters to eliminate: ")
eliminators = eliminators.strip().lower()

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

eliminationSet = set(eliminators)  # convert eliminators to set of characters to check

with open("XwiWordList.txt") as f:
    for line in f:
        line = line.strip().lower() # removes \n
        parts = line.split(';')
        word = parts[0].strip()
        score = parts[1]

        if not any(x in line for x in eliminationSet):
            print(line)

6. Sort word list by score and then by length

This example is different. There's no user input. It sorts a copy of your word list so the highest value words are on top. Within each score, words are sorted by length, and then alphabetically within each score/length group.

I'm not saying this particular sort is useful, but it demonstrates how to do it.

If you do re-sort your word list with a Python script, you probably want to run your program from the command line and then "pipe" it to a new text file. Open a Command Prompt, navigate to your folder, and type something like: main.py > MyNewTextFile.txt

# Define the class AnswerWord that, for each word in our word list,
# encapsulates the word, the length of the word, and the score.

class AnswerWord(object):
    def __init__(self, word="", score=0):
        self.word = word
        self.length = len(word)
        self.score = score

AnswerWordArray = []  # This array will hold a collection of AnswerWord objects

with open("XwiWordList.txt") as f:
    for line in f:
        parts = line.split(';')
        AnswerWordArray.append(AnswerWord(parts[0].strip().lower(), int(parts[1])))

# Sort by score (descending), length (ascending), and word (ascending) in one pass
AnswerWordArray.sort(key=lambda k: (-k.score, k.length, k.word))

for answerWord in AnswerWordArray:
    print(answerWord.word + ";" + str(answerWord.score))

7. A.I. can help

Suppose you're not an experienced programmer. Artificial Intelligence can (sometimes) do an amazing job.

I gave GitHub Copilot this prompt: "Write a Python function to read lines from a text file and print out the ones that are palindromes."

Here's the 7-line function it came up with, complete with comments!

def print_palindromes(filename):
    with open(filename, 'r') as file:
        for line in file:
            line = line.strip().lower()  # Remove leading/trailing white space and convert to lower case
            line = ''.join(ch for ch in line if ch.isalnum())  # Remove non-alphanumeric characters
            if line == line[::-1]:  # Check if the line is the same forward and backward
                print(line)

If you change the ch.isalnum (is alpha-numeric) to ch.isalpha, it will even work with files that include word scores.

Have fun

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

If you're serious about learning Python, XWord Info recommends PyCharm. The free "Community" version works fine. Visual Studio Code is also a great choice, and it's also free. Both are available for Windows, Mac, and Linux.

Python script has been copied to your clipboard.
XWord Info Home
XWord Info © 2007-2024, Jim Horne
0 ms