Q:

Python - Dictionary function only one entry for dictionary

Sorry if this is a silly question but I am new to python. I have a piece of code that was opening a text reading it, creating a list of words, then from that list create a dictionary of each word with a count of how many times it appears in the list of words. This code was working fine and was printing out the dictionary fine however when i put it in a function and called the function it returns the dictionary but only with one entry. Any ideas why, any help is much appreciated.
def createDict():

    wordlist = []

    with open('superman.txt','r', encoding="utf8") as superman:
                for line in superman:
                    for word in line.split():
                        wordlist.append(word)
                   #print(word)    

    table = str.maketrans("!#$%&()*+, ./:;<=>?@[\]^_`{|}~0123456789'“”-''—", 47*' ' )

    lenght = len(wordlist)
    i = 0

    while i < lenght:
            wordlist[i] = wordlist[i].translate(table)
            wordlist[i] = wordlist[i].lower()
            wordlist[i] = wordlist[i].strip()
            i += 1

    wordlist = list(filter(str.strip, wordlist))

    word_dict = {}

    for item in wordlist:
             if item in word_dict.keys():
                word_dict[item] += 1
    else:
                word_dict[item] = 1

    return(word_dict)

A:

try initializing the dictionary outside of the function and then using global inside the function. Is that one item in the dictionary the last iteration?

