How to print words that only cointain letters from a list?
<p>Hello I have recently been trying to create a progam in Python 3 which will read a text file wich contains 23005 words, the user will then enter a <strong>string of 9 characters</strong> which the program will use to create words and compare them to the ones in the text file.</p>

<p><strong>I want to print words which contains between 4-9 letters and that also contains the letter in the middle of my list</strong>. For example if the user enters the string "anitsksem" then the fifth letter "s" must be present in the word.</p>

<p>Here is how far I have gotten on my own:</p>

<pre><code># Open selected file &amp; read
filen = open("svenskaOrdUTF-8.txt", "r")

# Read all rows and store them in a list
wordList = filen.readlines()

# Close File
filen.close()

# letterList index
i = 0
# List of letters that user will input
letterList = []
# List of words that are our correct answers
solvedList = []

# User inputs 9 letters that will be stored in our letterList
string = input(str("Ange Nio BokstÃ¤ver: "))
userInput = False

# Checks if user input is correct
while userInput == False:
   # if the string is equal to 9 letters
   # insert letter into our letterList.
   # also set userInput to True
    if len(string) == 9:
        userInput = True
        for char in string:
            letterList.insert(i, char)
            i += 1

    # If string not equal to 9 ask user for a new input
    elif len(string) != 9:
        print("Du har inte angivit nio bokstÃ¤ver")
        string = input(str("Ange Nio BokstÃ¤ver: "))

# For each word in wordList
# and for each char within that word
# check if said word contains a letter from our letterList
# if it does and meets the requirements to be a correct answer
# add said word to our solvedList

for word in wordList:
    for char in word:
        if char in letterList:
            if len(word) &gt;= 4 and len(word) &lt;= 9 and letterList[4] in word:
                print("Char:", word)
                solvedList.append(word)
</code></pre>

<p>The issue that I run into is that instead of printing words which <strong>only</strong> contain letters from my <code>letterList</code>, it prints out words which contains <strong>at least one</strong> letter from my <code>letterList</code>. This also mean that some words are printed out multiple time, for example if the words contains multiple letters from <code>letterList</code>.</p>

<p>I've been trying to solve these problems for a while but I just can't seem to figure it out. I Have also tried using permutations to create all possible combinations of the letters in my list and then comparing them to my <code>wordlist</code>, however I felt that solution was to slow given the number of combinations which must be created.</p>

<pre><code>    # For each word in wordList
    # and for each char within that word
    # check if said word contains a letter from our letterList
    # if it does and meets the requirements to be a correct answer
    # add said word to our solvedList
    for word in wordList:
        for char in word:
            if char in letterList:
                if len(word) &gt;= 4 and len(word) &lt;= 9 and letterList[4] in word:
                    print("Char:", word)
                    solvedList.append(word)
</code></pre>

<p>Also since I'm kinda to new to python, if you have any general tips to share, I would really appreciate it.</p>

<p>You get multiple words mainly because you iterate through each character in a given word and if that character is in the <code>letterList</code> you append and print it.</p>

<p>Instead, iterate on a word basis and not on a character basis while also using the <code>with</code> context managers to automatically close files:</p>

<pre><code>with open('american-english') as f:
    for w in f:
        w = w.strip()
        cond = all(i in letterList for i in w) and letterList[4] in w
        if 9 &gt; len(w) &gt;= 4 and cond:
            print(w)
</code></pre>

<p>Here <code>cond</code> is used to trim down the <code>if</code> statement, <code>all(..)</code> is used to check if every character in the word is in <code>letterList</code>, <code>w.strip()</code> is to remove any redundant white-space.</p>

<p>Additionally, to populate your <code>letterList</code> when the input is <code>9</code> letters, <em>don't use <code>insert</code></em>. Instead, just supply the string to <code>list</code> and the list will be created in a similar, but noticeably faster, fashion:</p>

<p>This:</p>

<pre><code>if len(string) == 9:
    userInput = True
    for char in string:
        letterList.insert(i, char)
        i += 1
</code></pre>

<p>Can be written as:</p>

<pre><code>if len(string) == 9:
    userInput = True
    letterList = list(string)
</code></pre>

<p>With these changes, the initial <code>open</code> and <code>readlines</code> are not needed, neither is the initialization of <code>letterList</code>.</p>

python python-3.x for-loop comparison iteration