You have a list of potential dates, where each person is represented by a dictionary…
- User
You have a list of potential dates, where each person is represented by a dictionary containing their name, gender, age, hobbies, and city. Here's a sample of what the list might look like:
potential_dates = [ { "name": "Sasha", "gender": "male", "age": 18, "hobbies": ["rock music", "art"], "city": "Berlin", }, { "name": "Maria", "gender": "female", "age": 35, "hobbies": ["art"], "city": "Berlin", }, { "name": "Daniel", "gender": "non-conforming", "age": 50, "hobbies": ["boxing", "reading", "art"], "city": "Berlin", }, { "name": "John", "gender": "male", "age": 41, "hobbies": ["reading", "alpinism", "museums"], "city": "Munich", }, ]
Complete the select_dates() function that takes the potential_dates list as an argument and returns a string containing the names of people who meet all of the following criteria:
Age is greater than 30
Interested in art (i.e., "art" is in their hobbies list)
Living in Berlin
The names in the returned string should be separated by commas and a space.
For the potential_dates list provided above, the expected output is
Maria, Daniel
Hint by avatar Lin M return ", ".join() Was this hint helpful? Write a program in Python 3
def select_dates(potential_dates):
- Assistant
To solve this problem, we'll create a function
select_datesthat filters thepotential_dateslist based on the given criteria and returns the names as a comma-separated string.Here's how we'll approach it:
-
Initialize an Empty List: We'll start by creating an empty list called
namesto store the names of people who meet all the criteria. -
Iterate Over Potential Dates: We'll loop through each person in the
potential_dateslist. -
Check Each Criterion:
- Age: We'll check if the person's age is greater than 30.
- Hobbies: We'll check if "art" is in the person's list of hobbies.
- City: We'll check if the person lives in Berlin.
-
Collect Names: If a person meets all the above criteria, we'll append their name to the list.
-