Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Question 3: Age Filter Write a function that takes a tuple of lists you created in Question 2 and returns a tuple of lists of

Question 3: Age Filter

Write a function that takes a tuple of lists you created in Question 2 and returns a tuple of lists of people whose ages are less than or equals to given threshold.

def filter_age(lists, threshold):

"""Returns filtered tuple of lists

>>> lists = [['Alexa', 3], ['Betty', 14], ['Mike', 17], ['Marina', 20], ['Robert', 23], ['Siri', 45]]

>>> sorted(filter_age(lists, 23), key=lambda tup: tup[1])

[['Alexa', 3], ['Betty', 14], ['Mike', 17], ['Marina', 20], ['Robert', 23]]

>>> filter_age(lists, 1)

( )

>>> len(filter_age(lists, 45))

6

"""

# Your code is here #

Question 4: Put it back in a dictionary

Write a function that takes a tuple of lists you created in Question 3 and returns a dictionary (with the same keys as in Question 1) that does not contain filtered people.

def names_are_back(lists):

"""Returns a dictionary with filtered people

>>> lists = [['Alexa', 3], ['Betty', 14], ['Mike', 17], ['Marina', 20], ['Robert', 23], ['Siri', 45]]

>>> lists = filter_age(lists, 23)

>>> new_dictionary = names_are_back(lists)

>>> 'Marina' in new_dictionary['name']

True

>>> 45 in new_dictionary['age']

False

>>> new_dictionary['age'].sort()

>>> new_dictionary['age']

[3, 14, 17, 20, 23]

>>> lists = [['Alexa', 3], ['Betty', 14], ['Mike', 17], ['Marina', 20], ['Robert', 23], ['Siri', 45]]

>>> lists = filter_age(lists, 45)

>>> len(names_are_back(lists)['name'])

6

"""

# Your code is here #

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions