1

I'm trying to create a function that takes in a list and then edits the list as per a prior filter function (isValid) and also convert all words in the list into lowercase. Any words that don't fit the definition should be removed from the list.

The program works well when I return the edited list in cleanWords(), but the criteria do not allow me to use a return statement. When I don't return anything, the list that should be inputted into countFrequencies creates a "TypeError: 'NoneType' object is not iterable".

Could I then create a new function and pass the list through each argument separately? Or is there any other way of getting around this?

def cleanseWords(listOfWords):        
    for i in range(len(listOfWords)-1,-1,-1):    
        listOfWords[i] = listOfWords[i].lower()    
        if not isValid(listOfWords[i]):    
            listOfWords.remove(listOfWords[i])
    
def countFrequencies(listOfWords):    
    word_dict = {}    
    for word in listOfWords:    
        if word in word_dict:    
            word_dict[word] += 1                
        else:    
            word_dict[word] = 1    
    return word_dict


print(countFrequencies(cleanseWords(['aaaaaa','bbbbbb','cccc'])))
  • 1
    Why are you not returning anything? If you don't return anything, `None` is automatically returned, then you give that `None` to `countFrequencies`. If you want rely on mutating the argument, you'd need to break that last `print` line up and manually pass the list into both functions. – Carcigenicate Nov 16 '20 at 21:07
  • By that do you mean: cleanseWords(list), then countFrequencies(list) ? – greatwhitenorth Nov 16 '20 at 21:15
  • 1
    Assuming `list = ['aaaaaa','bbbbbb','cccc']`, yes. Don't use `list` as a variable name though. That's the name of an existing class. – Carcigenicate Nov 16 '20 at 21:16
  • Thanks so much for your help! When I do that, countFrequencies() doesn't operate on the filtered list, it just operates on the unedited list. – greatwhitenorth Nov 16 '20 at 21:31
  • Figured it out -- changing del to .remove() fixed everything! – greatwhitenorth Nov 16 '20 at 22:04

0 Answers0