Python Get List of Values from List of Keys

Rate this post

Problem Formulation

How to best determine the list of values associated to the keys?

Formally, for each key in the input list, you want to add d[key] to the list of values.

Here’s a minimal example:

# Input:
d = {'alice': 18, 'bob': 24, 'carl': 32}
keys = ['carl', 'bob']

# Output:
result = [32, 24]

Method 1: List Comprehension

To get the list of dictionary values from the list of keys, use the list comprehension statement [d[key] for key in keys] that iterates over each key in the list of keys and puts the associated value d[key] into the newly-created list.

d = {'alice': 18, 'bob': 24, 'carl': 32}
keys = ['carl', 'bob']

result = [d[key] for key in keys]

print(result)
# [32, 24]

This accesses each value separately in a concise one-liner code snippet. If you need a refresher on list comprehension, check out this video:

You can also read over the full guide on list comprehension.

However, you should know that if the key cannot be found, Python will raise a KeyError:

d = {'alice': 18, 'bob': 24, 'carl': 32}
keys = ['carl', 'bob', 'guido']

result = [d[key] for key in keys]

print(result)

The output of this code snippet is a KeyError:

...
    result = [d[key] for key in keys]
KeyError: 'guido'

To fix this without using a clunky try/except statement, simply end the list comprehension statement with a checking mechanism as an if clause:

d = {'alice': 18, 'bob': 24, 'carl': 32}
keys = ['carl', 'bob', 'guido']

result = [d[key] for key in keys if key in d]

print(result)
# [32, 24]

Method 2: For Loop

To get the list of values from a list of keys, you can also iterate over all keys in a simple for loop. The loop body simply consists of a list.append() statement that fills the initially empty list with the associated dictionary values obtained via d[key].

d = {'alice': 18, 'bob': 24, 'carl': 32}
keys = ['carl', 'bob']

result = []
for key in keys:
    result.append(d[key])

print(result)
# [32, 24]

This is the most common form used by beginners who are not yet familiar with list comprehension. However, although it’s not the most concise solution, I’d still consider it Pythonic due to its readability and simplicity.

You can learn more about the list.append() statement in the following video:

Method 3: Map Function

The most concise but least readable way to get the list of values from the list of dictionary keys is to use the expression list(map(d.get, keys)) that first applies the d.get(key) function for each key to get an iterable of associated values—and convert everything to a list using the built-in list() function.

d = {'alice': 18, 'bob': 24, 'carl': 32}
keys = ['carl', 'bob']

result = list(map(d.get, keys))

print(result)
# [32, 24]

To learn more about the map() function, feel free to watch the following video:

Or read the full guide on the map() function. It’ll be time well spent! 🙂

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!