3.8.1.1. Exercise: Creating and accessing dictionaries 1#

Here is a list of dictionaries that contain personal information about the participants of a research project:

participants = [
    {"ID": 101, "Sex": "F", "Height": 1.45, "Weight": 56.0},
    {"ID": 125, "Sex": "M", "Height": 1.72, "Weight": 72.0},
    {"ID": 126, "Sex": "M", "Height": 1.85, "Weight": 94.0},
    {"ID": 132, "Sex": "F", "Height": 1.82, "Weight": 80.0},
]

Write a code that calculates the average height among all participants. The number of participants may vary; therefore, your function should work for a list of any length.

Hide code cell content
# Calculate the sum
total = 0
for participant in participants:
    total += participant["Height"]

# Divide by the number of participants
average = total / len(participants)

# Show the result
print(f"The average height is {average} m.")
The average height is 1.71 m.