Values of one key from many dicts

EasyDictionary Programs · 82 of 94

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

Advertisement
Input
[{"name": "jos", "age": 30}, {"name": "david", "age": 25}, {"name": "virat", "age": 32}], key "age"
Output
[30, 25, 32]

Approach

  1. A comprehension.

Solution

Python
data = [{"name": "jos", "age": 30}, {"name": "david", "age": 25}, {"name": "virat", "age": 32}]print([d["age"] for d in data])
Output
[30, 25, 32]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs