Get words from our word list!

Published May 31, 2022, 3:20 p.m.

Welcome to the third (official) tutorial!  We've brought in the colorama module and figured out how to colorize the background around our words or letters.  Let's put a pin in that and go back to the list of words that we generated.  We would like to be able to read that list of words and ultimately use it to select the words for the wordle game.  (We will ignore, for the moment, the large percentage of unguessable words-- we will work on that later.) 

So I have a simple formula for opening a text file to read and I will not stray from that formula now!

First we will make a variable to represent our filename.  Then we will open the file for reading, read the lines and close the file!

fname = "five_letter_words_list.txt"
f = open(fname, 'r')
lines = f.readlines()
f.close()

You'll notice my brief moment of unsureness about how to close the file we opened.  I forgot to do it in my notes, but my memory kicked in and I remembered to do it for the tutorial.  Closing the file after reading the lines makes sure that we don't hog up memory for no reason.  Furthermore, closing the file makes sure that we don't make unwanted changes to the file. In some cases (when writing) the file will not be viewable until python closes it or exits.  All good reasons to go ahead and close the file when done with it.

Now if we print our lines to the screen we see something like the following:

...
'yirth, yocco, yodel, yogin, yoick, yojan, yokel, yoker, yolky, yomer, \n',
'youff, young, yourn, yours, youse, youth, youve, youze, yoven, yowie, \n', 
'yucca, yucky, yulan, yummy, yurta, zabra, zabti, zaman, zambo, zante, \n', 
'zanze, zapas, zayat, zayin, zebra, zebub, zeism, zeist, zemmi, zemni, \n', 
'zerda, zesty, ziara, zibet, ziega, ziffs, zihar, zimbi, zimme, zimmi, \n', 
'zinco, zippy, zirai, zloty, zocco, zoeal, zogan, zoism, zoist, zokor, \n', 
'zolle, zombi, zonal, zonar, zoned, zonic, zooid, zooks, zoons, zoril, \n', 
'zorro, zowie, zudda, zygal, zygon, zymic, zymin, ']

Our lines include a lot of newline characters (' \n') and spaces after the commas.  We will want to make sure that those characters do not get included in our list of words.

To do that we can create a wordlist and perform a few functions to make sure that the words get stripped of unwanted characters and that unwanted things don't make it into the list:

wordlist = []
for line in lines:
    words = [w.strip() for w in line.split(',') if w not in [' ', ' \n']]
    wordlist += words

So what we are doing above is looping through the lines, and creating a list of words for each line in the list.  The 'words' list is split on the comma character (',') AND we exclude items in the list if they are a space (' ') or a space + newline (' \n').

Finally, we add this small list of words to our global wordlist that we created outside the loop.

Now if we print our 'wordlist'  it looks like the following:

...
'yinst', 'yirth', 'yocco', 'yodel', 'yogin', 'yoick', 'yojan', 'yokel', 
'yoker', 'yolky', 'yomer', 'youff', 'young', 'yourn', 'yours', 'youse', 
'youth', 'youve', 'youze', 'yoven', 'yowie', 'yucca', 'yucky', 'yulan', 
'yummy', 'yurta', 'zabra', 'zabti', 'zaman', 'zambo', 'zante', 'zanze', 
'zapas', 'zayat', 'zayin', 'zebra', 'zebub', 'zeism', 'zeist', 'zemmi', 
'zemni', 'zerda', 'zesty', 'ziara', 'zibet', 'ziega', 'ziffs', 'zihar', 
'zimbi', 'zimme', 'zimmi', 'zinco', 'zippy', 'zirai', 'zloty', 'zocco', 
'zoeal', 'zogan', 'zoism', 'zoist', 'zokor', 'zolle', 'zombi', 'zonal', 
'zonar', 'zoned', 'zonic', 'zooid', 'zooks', 'zoons', 'zoril', 'zorro', 
'zowie', 'zudda', 'zygal', 'zygon', 'zymic', 'zymin']

This is what we want!  List of words, no spaces in the words, no items in the list that are newline characters or spaces.

Now, we could have tried to take care of the unwanted items in the list after the fact.  I find this method to be more elegant.  However, in the next tutorial, I will show you how to post-process the list in order to remove the unwanted items.

Thanks for watching and stick around for the next tutorial where we will show how to use our newly minted wordlist for the game!  Very soon we will get to the logic required for colorizing the letters.  Stay tuned!

skip_nextSelect a Random Wordle
  • Introduction to PyWordle

  • Get the latest version of Python (3.10.4)

  • Figure out all the 5 letter words (part 1)

  • Figure out all the 5 letter words (part 2)

  • Color letters with colorama!

  • Get words from our word list!
    (currently viewing)
  • Select a Random Wordle

  • Use a 'WHILE' loop for guessing

  • Start Comparing Letters in Words

  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!

  • Declaring Victory