Search code examples
pythonfunctionif-statementstring-length

problem with if statement using length function for filtering strings in python


I am trying to make a function which reviews all input words (variable amount of words using args) and returns all words over 10 characters long.

Here is my code:

def big_words(*n): #'*' allows variable amount of arguments

"""function to provide list of words with more than 10 characters.
allows multiple words to be processed"""

    return [item for item in n if len(str(n))>10]

big_words('substantiation', "boss", 'wallower', 'substantiation')

expected output:

['substantiation', 'substantiation']

actual output:

['substantiation', 'boss', 'wallower', 'substantiation']

why is the function not removing 'boss' and 'wallower'?

I am new to coding so all help appreciated


Solution

  • Because your if statement is asking for the length of n as a string, not the length of the word

    return [item for item in n if len(item) > 10]