Exercise: Looping through a dictionary

3.8.3.1. Exercise: Looping through a dictionary#

We affix an inclinometer on a person’s arm. We ask the person to reach different positions in the sagittal plane as shown in Fig. 3.10. Each position is reached twice.

Fig. 3.10 Different shoulder flexion angles.#

The inclinometer’s readings are stored in degrees in a dictionary as follows:

incline = {
    "Reference": -5.0,
    "TargetA_Repetition1": 32.3,
    "TargetB_Repetition1": 73.9,
    "TargetC_Repetition1": 112.1,
    "TargetA_Repetition2": 35.7,
    "TargetB_Repetition2": 82.1,
    "TargetC_Repetition2": 105.8,
}

Write code that creates a new dictionary named flexion, that calculates the shoulder flexion angle for each repetition of TargetA, TargetB and TargetC. This new dictionary (flexion), will have the following form:

{
    "TargetA_Repetition1": (flexion angle),
    "TargetB_Repetition1": (flexion angle),
    "TargetC_Repetition1": (flexion angle),
    "TargetA_Repetition2": (flexion angle),
    "TargetB_Repetition2": (flexion angle),
    "TargetC_Repetition2": (flexion angle),
}

The flexion angle is calculated as the incline in the target position, minus the incline at the reference position. Your code must adapt to any number of target positions, and any number of repetitions.

Hide code cell content
# Create an empty dictionary
flexion = {}

for key in incline:
    if key != "Reference":
        flexion[key] = incline[key] - incline["Reference"]

# Show the result
flexion
{'TargetA_Repetition1': 37.3,
 'TargetB_Repetition1': 78.9,
 'TargetC_Repetition1': 117.1,
 'TargetA_Repetition2': 40.7,
 'TargetB_Repetition2': 87.1,
 'TargetC_Repetition2': 110.8}