Question
Python (functional programming, lambda functions) Question 2: filter by name and height In this function, we will be exploring the filter() function in python. Remember
Python (functional programming, lambda functions)
Question 2: filter by name and height
In this function, we will be exploring the filter() function in python. Remember from readings that the filter function will return all elements of a list that satisfies a particular condition. This condition is passed into the filter function as the first argument in the form of a function (named or lambda).
Complete the function filter_by_name_and_height below. The function takes one parameter: people which is a list of dictionaries. Each dictionary in the list has two properties: name and height (height is in inches). Here is a sample structure of the list:
people = [ {'name': 'Andy', 'height': 180}, {'name': 'Bethany', 'height': 100}, {'name': 'Cassidy', 'height': 150} ]
Complete the function filter_by_name_and_height below to return a list of people (i.e. dictionaries) whose name is greater than 4 characters and whose height is greater than 120 inches. In the example above, only Cassidy's dictionary would be returned since the name attribute is greater than 4 characters and the height attribute is greater than 120 inches. The return value would look like [{'name': 'Cassidy', 'height': 150}] Note: Make sure to cast the output of the call to the filter() function to a list() type since filter() returns a filter_object. Also, make sure to use the filter() function. The autograder will check if you have used the filter() function.
def filter_by_name_and_height(people): """ Returns a list of people (i.e. dictionaries) that have names greater than 4 characters long and whose height is greater than 120 inches. Parameters ---------- people: list of dictionaries Returns ------- A list of dictionaries """ # YOUR CODE HERE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started