Question: Python Given a list of dictionaries representing people, where each dictionary contains the person's name, age, and gender, create a dictionary that maps each gender

Python

Given a list of dictionaries representing people, where each dictionary contains the person's name, age, and gender, create a dictionary that maps each gender to a list of tuples representing pairs of people who are the same age.

Here's an example input list:

people = [

{'name': 'Alice', 'age': 30, 'gender': 'female'},

{'name': 'Bob', 'age': 25, 'gender': 'male'},

{'name': 'Charlie', 'age': 30, 'gender': 'male'},

{'name': 'David', 'age': 25, 'gender': 'male'},

{'name': 'Eve', 'age': 30, 'gender': 'female'}

]

The output dictionary should look like this:

{

'female': [

((30, 'Alice'), (30, 'Eve'))

],

'male': [

((25, 'Bob'), (25, 'David')),

((30, 'Charlie'), (30, 'Alice')),

((30, 'Charlie'), (30, 'Eve'))

]

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!