Filter() and lambda functions
In the previous exercise, you used lambda functions to anonymously embed an operation within map()
. You will practice this again in this exercise by using a lambda function with filter()
, which may be new to you! The function filter()
offers a way to filter out elements from a list that don't satisfy certain criteria.
Your goal in this exercise is to use filter()
to create, from an input list of strings, a new list that contains only strings that have more than 6 characters.
This exercise is part of the course
Introduction to Functions in Python
Exercise instructions
- In the
filter()
call, pass a lambda function and the list of strings,fellowship
. The lambda function should check if the number of characters in a stringmember
is greater than 6; use thelen()
function to do this. Assign the resulting filter object toresult
. - Convert
result
to a list and print out the list.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a list of strings: fellowship
fellowship = ['frodo', 'samwise', 'merry', 'pippin', 'aragorn', 'boromir', 'legolas', 'gimli', 'gandalf']
# Use filter() to apply a lambda function over fellowship: result
____ = filter(____, ____)
# Convert result to a list: result_list
____
# Print result_list
print(result_list)