Q:

Python for loop not iterating

I'm trying to loop through a list of strings and add them to a dictionary if their length equals a length input by the user. When the last loop runs, it only runs one time. I know this because the first word in the dictionary is 8 characters long, and when the user input is 8, it prints just that word, and not the other 8 character words. If the input is 3, an empty dictionary is printed. Why is my loop not iterating through all of the words in the list linelist?
wordLength = raw_input("Enter a word length ")
word_dict = {}
infile = open("dictionary.txt")

for line in infile:
    line = line.strip()
    linelist = line.split(" ")

for word in linelist:
    if len(word) == int(wordLength):
        if len(word) in word_dict:
            word_dict[len(word)] = word_dict[len(word)].append(word)
        else:
            word_dict[len(word)] = word

print word_dict

A:

Each time your first loop runs, it sets linelist to a new value, overwriting any old value.  After that first loop runs, linelist will contain only the split result from the last line of the file.  Every time you process one line of the file, you are throwing away whatever you did with the previous line.
If you want to build a list of all words in the dictionary file, you need to make a list and append to it on each iteration of your for line in infile loop.
Also, it doesn't make much sense to use split on each line if each line is just one word, since there will be no splitting to be done.

