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.
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:
Output:
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:
Output:
You can also provide a default value:
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:
Output:
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.
Output:
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:
This gives you one place to define missing-key behavior.
Equivalent Idea in Other Languages
The pattern is the same in JavaScript:
And in Java with a Map:
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
- Get modulo from two 4x64bit integer arrays
- Get node list from random walk in networkX
- Get number of messages in an Amazon SQS Queue
- Get Object From Collection By Attribute
- Get only part of an Array in Java?
- Get replica set of the deployment
- Get the biggest chronological drop, min and max from an array with On
- Get the closest value for combinations of an array JS

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