Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A fucntion that checks the length of each word in a list and returns a new list depending on the condition
- def right_words(word): #define the function
- return len(word)==3 #state a condition which must be True for any of the element in the list
- words = ['cat', 'dog','bean','ace', 'heart'] # assign list to a variable
- #use the filter function to apply the function defined above to the list
- #and then convert it to a list
- print(list(filter(right_words,words)))
- def right_words(word):
- return len(word)==5
- words = ['cat', 'dog','bean','ace', 'heart']
- print(list(filter(right_words,words)))
- def right_words(word):
- return len(word)==4
- words = []
- print(list(filter(right_words,words)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement