list processing
key-value retrieval
dictionaries
data extraction
programming tips

Get list of values for list of keys

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Getting a list of values from a dictionary by using a list of keys is a small task that appears everywhere in application code, scripts, and data processing. The main design choice is not the lookup itself, but how you want to handle missing keys: fail fast, skip them, or substitute defaults.

The Direct Python Approach

If every key is guaranteed to exist, a list comprehension is the cleanest solution:

python
1data = {
2    "name": "Ava",
3    "role": "Engineer",
4    "city": "Toronto",
5}
6
7keys = ["name", "city"]
8values = [data[key] for key in keys]
9
10print(values)

Output:

text
['Ava', 'Toronto']

This is concise and fast. If a key is missing, Python raises KeyError, which is often the right behavior when missing data is a bug.

Handling Missing Keys Safely

If missing keys are expected, use dict.get() instead:

python
1data = {
2    "name": "Ava",
3    "role": "Engineer",
4}
5
6keys = ["name", "city", "role"]
7values = [data.get(key) for key in keys]
8
9print(values)

Output:

text
['Ava', None, 'Engineer']

You can also provide a default value:

python
values = [data.get(key, "UNKNOWN") for key in keys]
print(values)

That is useful when you want positional alignment between the original key list and the returned values, even if some keys are absent.

Preserving Order Matters

The output list follows the order of the requested keys, not the order of the dictionary. That is usually the desired behavior:

python
1data = {"a": 10, "b": 20, "c": 30}
2keys = ["c", "a"]
3
4print([data[k] for k in keys])

Output:

text
[30, 10]

This is important in pipelines where the key list defines a schema, display order, or export column order.

Filtering Only Existing Keys

Sometimes you want to drop missing keys entirely instead of inserting placeholders.

python
1data = {"a": 10, "b": 20}
2keys = ["a", "x", "b", "y"]
3
4values = [data[key] for key in keys if key in data]
5print(values)

Output:

text
[10, 20]

This changes the length of the result, so it is appropriate only when positional alignment does not matter.

Scaling the Pattern

For a few keys, any method is fine. For larger workflows, clarity still matters more than micro-optimization because dictionary lookups are already efficient.

If the same lookup pattern repeats across a codebase, wrapping it in a helper may make intent clearer:

python
1def values_for_keys(mapping, keys, default=None):
2    return [mapping.get(key, default) for key in keys]
3
4
5record = {"id": 7, "name": "Ava"}
6print(values_for_keys(record, ["name", "email", "id"], default="N/A"))

This gives you one place to define missing-key behavior.

Equivalent Idea in Other Languages

The pattern is the same in JavaScript:

javascript
1const data = { name: "Ava", role: "Engineer", city: "Toronto" };
2const keys = ["city", "name"];
3
4const values = keys.map((key) => data[key]);
5console.log(values);

And in Java with a Map:

java
1List<String> keys = List.of("name", "city");
2Map<String, String> data = Map.of("name", "Ava", "city", "Toronto");
3
4List<String> values = keys.stream()
5    .map(data::get)
6    .toList();

The real issue is always the same: what should happen when a requested key is missing.

Common Pitfalls

One common mistake is using direct indexing when missing keys are normal input. That turns a recoverable case into an exception.

Another mistake is using get() with a default when missing keys should actually be treated as invalid data. Silent defaults can hide upstream bugs.

People also confuse "return values for existing keys" with "return a value for every requested key." Those are different requirements and produce different output lengths.

Finally, avoid recomputing expensive derived values inside the lookup loop if the dictionary already contains the data. The retrieval step should stay simple.

Summary

  • Use a list comprehension with direct indexing when all keys must exist.
  • Use dict.get() when missing keys are expected or defaults are required.
  • The result order follows the requested key list, not the dictionary order.
  • Decide early whether missing keys should raise, default, or be skipped.
  • Small lookup helpers can make missing-key policy consistent across a codebase.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.