Extract a Field From a List of Dicts

Easy ⏱ 8 min 85% acceptance ★★★★★ 4.5
Write extract_field(records, field) where records is a list of dictionaries. Return a list of the values found under field for each record, in order. If a record does not contain field, use None in its place.

Examples

Example 1
Input
records = [{'name': 'Ana', 'age': 30}, {'name': 'Bo'}], field = 'age'
Output
[30, None]
Explanation

Ana has an age of 30; Bo has no age key so None is used.

Constraints

  • 0 <= len(records) <= 10^4

Topics

ListsDicts

Companies

LinkedInSalesforce

Hints

Hint 1

dict.get(field) returns None automatically when the key is missing.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.