Extract Dictionary Values as a Python List
Last Updated :
18 Jan, 2025
To extract dictionary values from a list, we iterate through each dictionary, check for the key's presence, and collect its value. The result is a list of values corresponding to the specified key across all dictionaries.
For example, given data = {'a': 1, 'b': 2, 'c': 3}, the output will be [1, 2, 3]. In this article, we will explore different methods to achieve this.
Using list comprehension
List comprehension is a efficient way to extract values from dictionaries in a list. By checking if the key exists, it safely retrieves and adds the corresponding value to the result, ensuring efficient and concise data processing.
Python
li = [
{'name': 'improvement', 'letters': 25, 'city': 'Hyderabad'},
{'name': 'geeksforgeeks', 'letters': 30, 'city': 'United states'},
{'name': 'author', 'letters': 22, 'city': 'Japan'}
]
k = 'name' # key
# Extract values
ans = [i[k] for i in li if k in i]
print(ans)
Output['improvement', 'geeksforgeeks', 'author']
Explanation:
for i in li
: This iterates through each dictionary in the list li .if k in i
: This ensures the key k
exists in the current dictionary to avoid errors.i[k]
: This retrieves the value for the key k .
Using map()
By combining map with filter, we can efficiently extract specific values from a list of dictionaries without using explicit loops. This approach is particularly useful when working with large datasets as it enhances performance.
Python
li = [
{'name': 'improvement', 'letters': 25, 'city': 'Hyderabad'},
{'name': 'geeksforgeeks', 'letters': 30, 'city': 'United states'},
{'name': 'author', 'letters': 22, 'city': 'Japan'}
]
k = 'city' # key
ans = list(map(lambda i: i[k], filter(lambda i: k in i , li)))
print(ans)
Output['Hyderabad', 'United states', 'Japan']
Explanation:
filter ()
checks if the keyk
exists in each dictionary and returns only dictionaries containing the key k .map
() extracts the value for the keyk
from each dictionary returned by filter
.
Using generator expression
Generator expressions provide an efficient way to extract values from dictionaries in a list, as they only generate values when needed. This is ideal for large datasets, as it minimizes memory usage by avoiding the creation of an entire intermediate list in memory.
Python
li = [
{'name': 'improvement', 'letters': 25, 'city': 'Hyderabad'},
{'name': 'geeksforgeeks', 'letters': 30, 'city': 'United states'},
{'name': 'author', 'letters': 22, 'city': 'Japan'}
]
k = 'name' # key
ans = list(i[k] for i in li if k in i)
print(ans)
Output['improvement', 'geeksforgeeks', 'author']
Explanation:
- Generator expression: This loops through each dictionary in
li
and checks if the key k
exists. - If condition:
if
k
in
i
ensures the key is present in the dictionary. i[k]
retrieves the value associated with the key k .- list() converts the values generated by the generator expression into a list.
Using reduce()
reduce from functools lets us apply a custom function to combine values in an iterable. It's useful for advanced cases where we need custom logic though, not as common as list comprehension for simpler tasks.
Python
from functools import reduce
li = [
{'name': 'improvement', 'letters': 25, 'city': 'Hyderabad'},
{'name': 'geeksforgeeks', 'letters': 30, 'city': 'United states'},
{'name': 'author', 'letters': 22, 'city': 'Japan'}
]
k = 'name' # key
ans = reduce(lambda acc, i: acc + [i[k]] if k in i else acc, li, [])
print(ans)
Output['improvement', 'geeksforgeeks', 'author']
Explanation:
reduce
applies the lambda function across all elements of the list li .- Lambda adds the value of
i[k]
toacc
if k
exists in the dictionary, otherwise keeps acc
unchanged.
Similar Reads
Get Python Dictionary Values as List - Python We are given a dictionary where the values are lists and our task is to retrieve all the values as a single flattened list. For example, given the dictionary: d = {"a": [1, 2], "b": [3, 4], "c": [5]} the expected output is: [1, 2, 3, 4, 5]Using itertools.chain()itertools.chain() function efficiently
2 min read
Ways to extract all dictionary values | Python While working with Python dictionaries, there can be cases in which we are just concerned about getting the values list and don't care about keys. This is yet another essential utility and solution to it should be known and discussed. Let's perform this task through certain methods. Method #1 : Usin
3 min read
Get List of Values From Dictionary - Python We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t
2 min read
Python Print Dictionary Keys and Values When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values.Example: Using print() MethodPythonmy_dict = {'a': 1, 'b': 2, 'c': 3} print("Keys:", l
2 min read
Dictionary keys as a list in Python In Python, we will encounter some situations where we need to extract the keys from a dictionary as a list. In this article, we will explore various easy and efficient methods to achieve this.Using list() The simplest and most efficient way to convert dictionary keys to lists is by using a built-in
2 min read