Python Practice › Dictionary Programs › Program 63
Values for one key from a list of dicts
Write a Python program to extract the values for a key from a list of dictionaries.
Input
[{"t20": 50, "odi": 70}, {"t20": 40, "odi": 10}, {"t20": 30, "odi": 0}, {"t20": 45, "odi": 65}], key "odi"Output
[70, 10, 0, 65]
Approach
[d["odi"] for d in data].
Solution
data = [{"t20": 50, "odi": 70}, {"t20": 40, "odi": 10}, {"t20": 30, "odi": 0}, {"t20": 45, "odi": 65}]print([d["odi"] for d in data])Output
[70, 10, 0, 65]
Learn the concept
New to this topic? Read the lesson, then come back.