Files
lab_weather/friend_functions.py
angelotr b6eb4ea7ef I replaced all the raise NotImplementedError() lines with working code that loops through the list of dictionaries to find names, emails, and favorite colors, counts people and colors, and creates a dictionary showing how many people like each color.
I got stuck at first trying to loop through the list of dictionaries to
find specific information, since I it has been a while I have
had accessing values inside nested structures. I got un-stuck by
reviewing examples of how to use keys in dictionaries and by printing
out parts of the data step by step to see how it was organized.
2025-10-14 00:35:15 -04:00

51 lines
1.2 KiB
Python

# friend_functions.py
# ------------
# By MWC Contributors
#
# Each of the functions below expects a list of dictionaries as its first
# argument. Two examples of the expected input are provided in people.family
# and people.friends.
# Your job is to complete these functions. Remove the NotImplementedError from
# each and instead write code which returns the expected values.
def count_people(people):
return len(people)
def get_email(people, name):
for person in people:
if person["name"] == name:
return person["email"]
return None
def count_favorite_colors(people, name):
for person in people:
if person["name"] == name:
return len(person["favorite_colors"])
return None
def people_who_like_color(people, color):
return [person for person in people if color in person["favorite_colors"]]
def count_people_who_like_color(people, color):
return len(people_who_like_color(people, color))
def get_color_dict(people):
color_dict = {}
for person in people:
for color in person["favorite_colors"]:
color_dict[color] = color_dict.get(color,0) + 1
return color_dict