Pick a random big word from a list of words using Python I am trying to randomly pick a large word (7 letters or more in length) from a large text file of words.

I know that there has to be a better and more efficient way to do this.

def goalWord():
    with open("words.txt","r") as allWords:
        wordList = allWords.read()
        wordList = wordList.split()
        bigWords = []
        for word in wordList:
            if len(word) &gt; 6:
                bigWords.append(word.upper())
        return random.choice(bigWords)


Here is the sample input:


A
a
aa
aal
aalii
aam
Aani
aardvark
aardwolf
Aaron
Aaronic
Aaronical
Aaronite
Aaronitic
Aaru
Ab
aba
Ababdeh
Ababua
abac
abaca
abacate
abacay



Output would be a random word from words.txt that is greater than 6 characters in length.
Comments


Python functions are written in snake_case.
You could use function parameters for word length and filename.
You could use iterators and generators to avoid loading the whole file into memory.
If you read the file line by line, you need to take care with the trailing newline character: len("test\n") is 5.


Refactoring

Here's a way to rewrite your function. Thanks to a generator, the script only loops once over every line:

import random


def goal_word(min_length=7, filename="words.txt"):
    with open(filename) as wordbook:
        words = (line.rstrip('\n') for line in wordbook)
        large_words = [word for word in words if len(word) &gt;= min_length]
    return random.choice(large_words)

print(goal_word(7))
# evening
print(goal_word(15))
# circumnavigations


Optimization

@etchesketch commented that rstrip is called on every line even though it's only needed for one word. Here's a variation:

import random


def goal_word(min_length=7, filename="words.txt"):
    min_line_length = min_length + 1
    with open(filename) as wordbook:
        large_words = [line for line in wordbook if len(line) &gt;= min_line_length]
    return random.choice(large_words).rstrip('\n')

print(goal_word(7))
# jauntily
print(goal_word(15))
# fundamentalists


On a dictionary of English words (/usr/share/dict/american-english), this function is 3 times faster than the previous one.

Exception handling

In the above examples, goal_word(30) fails with IndexError: Cannot choose from an empty sequence. There's no indication that the desired length is too long.

Before calling random.choice, the script could simply check that large_words isn't empty:

import random


def goal_word(min_length=7, filename="words.txt"):
    min_line_length = min_length + 1
    with open(filename) as wordbook:
        large_words = [line for line in wordbook if len(line) &gt;= min_line_length]
    if large_words:
        return random.choice(large_words).rstrip('\n')
    else:
        raise ValueError("No word found with at least %s characters." % min_length)


print(goal_word(7))
# jauntily
print(goal_word(15))
# insurrectionist's
print(goal_word(30))
# ValueError: No word found with at least 30 characters.