Exercise: Adding entries to dictionaries

3.8.2.1. Exercise: Adding entries to dictionaries#

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 adds a new key BMI to each of these dictionaries, where:

\[\text{BMI} = \text{weight}/\text{height}^2\]
Hide code cell content
for participant in participants:
    participant["BMI"] = participant["Weight"] / (participant["Height"] ** 2)

# Show the result
participants
[{'ID': 101,
  'Sex': 'F',
  'Height': 1.45,
  'Weight': 56.0,
  'BMI': 26.634958382877528},
 {'ID': 125,
  'Sex': 'M',
  'Height': 1.72,
  'Weight': 72.0,
  'BMI': 24.337479718766904},
 {'ID': 126,
  'Sex': 'M',
  'Height': 1.85,
  'Weight': 94.0,
  'BMI': 27.465303140978815},
 {'ID': 132,
  'Sex': 'F',
  'Height': 1.82,
  'Weight': 80.0,
  'BMI': 24.151672503320853}]