Filter Long Words

Write a Python function to find the list of words that are longer than ‘n’ from a given list of words.

Example 1:

Input: ['hello', 'world', 'python', 'is', 'awesome'], 5 
Output: ['python', 'awesome']

Example 2:

Input: ['apple', 'banana', 'cherry', 'date', 'elderberry'], 6 
Output: ['elderberry']

Use a loop to iterate through each word in the list and Python’s len() function to check the length of each word.

def filter_long_words(words, n):
    return [word for word in words if len(word) > n]

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!