3.8.1.2. Exercise: Creating and accessing dictionaries 2#
We have 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 iterates through all elements of this list to create a new list that contains only the participants IDs: [101, 125, 126, 132]
.
Show code cell content
# Initialize an empty list
ids = []
for participant in participants:
ids.append(participant["ID"])
# Show the result
ids
[101, 125, 126, 132]