Values for one key from a list of dicts

EasyDictionary Programs · 63 of 94

Write a Python program to extract the values for a key from a list of dictionaries.

Advertisement
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

  1. [d["odi"] for d in data].

Solution

Python
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.

More dictionary programs

← All 94 Dictionary Programs