Python
Data Structures
List Comprehension
Dictionaries
Programming

Getting a list of values from a list of dicts

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Extracting values from a list of dictionaries is a common Python task in API processing, ETL scripts, and reporting jobs. The best method depends on whether keys are guaranteed, whether you need speed, and how much post-processing you plan to do. With a few patterns, you can keep this operation both fast and readable.

Basic Extraction with List Comprehension

If every dictionary has the key, list comprehension is concise and usually fastest for straightforward extraction.

python
1rows = [
2    {"id": 1, "name": "Ava", "score": 91},
3    {"id": 2, "name": "Noah", "score": 87},
4    {"id": 3, "name": "Mia", "score": 95},
5]
6
7names = [row["name"] for row in rows]
8print(names)  # ['Ava', 'Noah', 'Mia']

This should be your default when input schema is trusted and stable.

Safe Extraction When Keys May Be Missing

Real datasets are often messy. Use dict.get to avoid KeyError and optionally filter out missing values.

python
1rows = [
2    {"id": 1, "name": "Ava"},
3    {"id": 2},
4    {"id": 3, "name": "Mia"},
5]
6
7names_with_none = [row.get("name") for row in rows]
8names_only = [value for value in names_with_none if value is not None]
9
10print(names_with_none)  # ['Ava', None, 'Mia']
11print(names_only)       # ['Ava', 'Mia']

If the downstream consumer requires complete data, keep None values and validate later with explicit error reporting.

Extract Multiple Fields in One Pass

When you need several values together, extract tuples or small dictionaries in one pass to avoid repeated loops.

python
1rows = [
2    {"id": 1, "name": "Ava", "score": 91},
3    {"id": 2, "name": "Noah", "score": 87},
4    {"id": 3, "name": "Mia", "score": 95},
5]
6
7summary = [(row["id"], row["name"], row["score"]) for row in rows]
8print(summary)

For larger codebases, this can improve clarity because each output record has a stable shape.

Handling Nested Keys

Some payloads place values in nested dictionaries. Use helper functions to avoid repetitive chained lookups.

python
1def deep_get(mapping: dict, path: list[str], default=None):
2    current = mapping
3    for key in path:
4        if not isinstance(current, dict) or key not in current:
5            return default
6        current = current[key]
7    return current
8
9rows = [
10    {"user": {"profile": {"city": "Toronto"}}},
11    {"user": {"profile": {}}},
12]
13
14cities = [deep_get(row, ["user", "profile", "city"], default="unknown") for row in rows]
15print(cities)

A helper keeps edge-case handling centralized and easier to test.

Using operator.itemgetter for Readability

itemgetter is useful when key extraction appears repeatedly across utility modules.

python
1from operator import itemgetter
2
3rows = [
4    {"id": 1, "name": "Ava", "score": 91},
5    {"id": 2, "name": "Noah", "score": 87},
6    {"id": 3, "name": "Mia", "score": 95},
7]
8
9get_name = itemgetter("name")
10names = [get_name(row) for row in rows]
11print(names)

The performance difference from a direct key access is usually small, so choose the style your team finds clearer.

Scaling to Tabular Workloads with Pandas

If extraction is part of a broader table pipeline, convert once to a DataFrame and use column operations.

python
1import pandas as pd
2
3rows = [
4    {"id": 1, "name": "Ava", "score": 91},
5    {"id": 2, "name": "Noah", "score": 87},
6    {"id": 3, "name": "Mia", "score": 95},
7]
8
9df = pd.DataFrame(rows)
10names = df["name"].tolist()
11print(names)

This is especially useful when you also need filtering, grouping, or joins in the same flow.

Common Pitfalls

  • Assuming every dictionary has the key can crash production jobs with KeyError.
  • Running multiple separate loops for related fields wastes time and complicates maintenance.
  • Silently dropping missing values can hide upstream data-quality problems.
  • Mixing row-oriented loops and DataFrame operations in one function can hurt readability.
  • Forgetting type normalization may cause inconsistent output when values come from mixed data sources.

Summary

  • Use list comprehension with direct key access for clean, trusted schemas.
  • Use get when key presence is uncertain and decide deliberately how to handle missing values.
  • Extract multiple fields in one pass when outputs share lifecycle.
  • Prefer Pandas only when you already need tabular transformations.
  • Keep extraction rules explicit so data quality issues are visible early.

Course illustration
Course illustration

All Rights Reserved.