Open In App

Extract Dictionary Values as a Python List

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The dictionary is a collection of data items in the key-value pair and represented by the braces { }. The list is also a data type used to store the date and represented by the braces [ ]. The built-in Python method items() are used to retrieve all the keys and corresponding values. We can print the dictionary’s keys and values by combining the items() method with a for loop.

Print All Values Of A Given Key Of Dictionaries In A List

Below are some of the ways by which we can print all values of a given key of dictionaries in a list in Python:

  1. Using a for Loop
  2. Using List Comprehension
  3. Using the lambda and filter functions
  4. Using the ‘operator‘ module

Using for Loop

The for loop iterates the dictionaries present in the list. If the condition checks whether the given key is present in the dictionary or not. If it is present in the dictionary, then it will return the value of the key. If it is not present in the dictionary, it will not print anything.

Python3




list_of_dicts = [
    {'name': 'improvement', 'letters': 25, 'city': 'Hyderabad'},
    {'name': 'geeksforgeeks', 'letters': 30, 'city': 'United states'},
    {'name': 'author', 'letters': 22, 'city': 'Japan'}]
 
key_to_print = 'name'
 
ans = []
# iterated the list of dictionary
for d in list_of_dicts:
    if key_to_print in d:
        ans.append(d[key_to_print])
 
print(ans)


Output

['improvement', 'geeksforgeeks', 'author']


Using List Comprehension

Here, we are checking whether entered key is present in dictionary or not using list comprehension method and return the value of the key if exist in the dictionary.

Python3




list_of_dicts = [
    {'name': 'improvement', 'letters': 25, 'city': 'Hyderabad'},
    {'name': 'geeksforgeeks', 'letters': 30, 'city': 'United states'},
    {'name': 'author', 'letters': 22, 'city': 'Japan'}]
 
key_to_print = 'city'
 
values = [d[key_to_print] for d in list_of_dicts if key_to_print in d]
print(values)


Output

['Hyderabad', 'United states', 'Japan']


Using the lambda and filter functions

The filter() function filters the dictionaries in list of dictionaries where key is present. map() function retrieve the value of the key from each dictionary and result of map function is converted in to list using list() function.

Python3




list_of_dicts = [
    {'name': 'improvement', 'letters': 25, 'city': 'Hyderabad'},
    {'name': 'geeksforgeeks', 'letters': 30, 'city': 'United states'},
    {'name': 'author', 'letters': 22, 'city': 'Japan'}]
 
key_to_print = 'name'
values = list(map(lambda d: d[key_to_print], filter(lambda d: key_to_print in d, list_of_dicts)))
print(values)


Output

['improvement', 'geeksforgeeks', 'author']


Using the operator module

In this example, itemgetter(desired_key) creates a callable object that extracts the value associated with the specified key from a dictionary. The map() function applies this callable to each dictionary in list_of_dicts, and the result is converted to a list.

Python3




from operator import itemgetter
 
# Sample list of dictionaries
list_of_dicts = [{'key1': 10, 'key2': 20}, {'key1': 30, 'key2': 40}, {'key1': 50, 'key2': 60}]
 
# Key to extract values from
desired_key = 'key1'
 
# Using itemgetter
result = list(map(itemgetter(desired_key), list_of_dicts))
 
print(result)


Output

[10, 30, 50]




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads