Select a Random Wordle

Published June 3, 2022, 1:50 p.m.

Now that have a list of words to use for our Wordle game, we need to select a word from the list!

We want the word to be a real word and we want the word to be unknown to us so I suggest we use the random module to make this selection!  But first!...

As promised, I wanted to show you how you could remove all of the unwanted items from a list after the fact.  (The "less-elegent way").   The so-called "elegant" way is shown below:

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

Without the "if-statement" in the line that produces the list "words", the list of words would include several space+newline character items like the following:

 ...
xurel, \n', 'xylan, xylem, xylic, xylol, xylon, xylyl, xyrid, xysti, yabbi,
 yabby, \n', 'yacal, yacca, yacht, yagua, yahan, yahoo, yaird, yakin, yakka,
 yalla, \n', 'yamen, yampa, yamph, yanky, yaply, yapok, yappy, yarak, yaray,
 yarke, \n', 'yarly, yarth, yauld, yawny, yeara, yeard, yearn, yeast, yerba,
 yerga, \n', 'yerth, yesso, yesty, yeuky, yeven, yezzy, ygapo, yield, yince,
 yinst, \n', '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, ']

There is also a lone space at the very end which is why we wanted to exclude two items from the list.  If we weren't clever enough to include that if-statement in our one-line definition of the words then our code would look like this:

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

This produces a wordlist that looks like the following:

 ... '', 'yerth', 'yesso', 'yesty', 'yeuky', 'yeven', 'yezzy', 'ygapo', 
'yield', 'yince', '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', '']

If you look closely, you'll find several items in the list that are empty strings or just two single quotes: ''.

The reason for this is that we used .strip() to remove whitespace from our characters.  Obviously spaces count as whitespace but so does a newline character.  Therefore, we are left with a bunch of words and also a bunch of empty character strings as items in our list.  Yuck.

We could try to remove the empty strings using wordlist.remove('') alone, however, the remove attribute for a list only removes a single instance of the item in question.  Furthermore, it gives you an error if you try to remove something that is not there. 

We desire to remove all instances of the empty string in our list.  What comes to my mind are the python class sets.

If we convert our list to a set, it will collapse all instances of the empty string into a single instance.  For good measure, if by some quirk we happen to have a duplicated word in our list it would fix that too!

We ultimately want our wordlist as a proper list in python so we can convert it to a set and then back to a list in a single line:

wordlist = list(set(wordlist))

Then we can safely remove the ONLY empty string in our list:

wordlist.remove('')

This leaves us with something like the following:

...
'picot', 'domal', 'reesk', 'grump', 'dhyal', 'tutti', 'exalt', 'gagee', 
'thymy', 'tawny', 'belly', 'antes', 'short', 'wafty', 'walth', 'fodda', 
'friar', 'idant', 'harem', 'stout', 'triad', 'cauch', 'flamb', 'style', 
'betty', 'blash', 'woman', 'snead', 'halal', 'yucca', 'dobby', 'skeed', 
'oasis', 'javer', 'kraut', 'ovoid', 'immit', 'aural', 'butyl', 'sarpo', 
'crome', 'caffa', 'murga', 'begum', 'wodgy', 'ender', 'berat', 'scran', 
'besin', 'algal', 'retan', 'shaku', 'morat', 'horde', 'aulic', 'duchy', 
'kokio', 'vairy', 'crypt', 'netop', 'arupa', 'ology', 'kenaf', 'teems', 
'khaki', 'mucky', 'simar', 'irade', 'ecoid', 'sepic', 'block', 'twain', 
'unpin', 'ainoi', 'foehn', 'aflow', 'bolar', 'acari', 'aping', 'malik']

A nice, tight list with no unwanted items (if you can still excuse words like 'malik' and 'sarpo').  Notice now that the words are not in alphabetical order!  This happens because sets in python are non-indexed meaning they don't carry any particular order.  When we convert the list to a set and then convert the unordered set back to a list the new list assigns a new order to the words with a new index.  I don't show it in my tutorial, but you could easily resort the list using wordlist.sort().

...
'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']

But enought about that!  I say, convert your wordlist definition back to this:

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

And be done with it.

Now, onto randomly selecting a word from our list!  That is the most simple thing we'll do all day.

If you haven't already go to the top of your code and add:

from random import choice

'choice' is a member of the random module that randomly selects a single item from a list.  Thus:

word = choice(wordlist)

is a sufficient definition to get a random word from our wordlist.  However, since we know that our wordlist has a bunch of crap words in it, we might like to make our own smaller word list just for while we are creating this program.

TEST_WORD_LIST = [
'error',
'apple',
'cruel',
'ionic',
'pizza'
]

Now if we change our definition for word to:

word = choice(TEST_WORD_LIST)

print(word)

and print the word at the end of our code we can make sure that we are selecting a word from our tiny list and that the selection is random.  Go ahead, run the program a few times until you've selected all of the words from the list at least once.

Okay!  We've shortened our list of approved words and have a method for randomly selecting from the list.  Next time we will finally start working on the logic required to make a Wordle-style game.  Stay tuned!

 

 

 

skip_nextUse a 'WHILE' loop for guessing
  • 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!

  • Select a Random Wordle
    (currently viewing)
  • Use a 'WHILE' loop for guessing

  • Start Comparing Letters in Words

  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!

  • Declaring Victory