Signup/Sign In

How to Get a List of Values from a List of Dictionary?

In this article, we will learn how to get a list of values from a dictionary in Python. We will use some built-in functions, simple approaches, and some custom code as well to understand different ways. Let's first have a quick look over what is a list and a dictionary in Python.

Python Lists

Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values. For example,

list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]

Python Dictionary

Dictionaries are Python's other built-in data type and it is also known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. Data inside a dictionary can be of any type say, integer, string or a float value, etc.. A dictionary can be defined using any variable name and then assigning different key-value pairs in curly braces. For example,

dict1 = {"A" : 1, "B" : 2, "C" : 3}
dict2 = {"Name": "Jeffery", 1: [2, 4, 3]}
dict3 = {1: "first", 2: "second"}

Get a List of Values from a List of Dictionary

In Python, a dictionary is represented in the form of key-value pairs. List of dictionary means multiple dictionaries are enclosed within square brackets. Each element has a key associated with it. You can easily find the list of all the values in the given list of dictionaries, but you can even find the list of values associated with a given key. In this article, we are assuming that every dictionary has a value and a key. Let us discuss the different ways of getting a list of values.


Example: Use List Comprehension

This method uses a list comprehension technique to find the list of values. It takes a key as input and returns a list containing the corresponding value for each occurrence of the key in each dictionary in the list using for loop. This method is more elegant and pythonic than others.

#list of dictionaries
books = [
{"title": "Pride and Prejudice ", "author": "Jane Austen"},
{"title": "David Copperfield", "author": "Charles Dickens"},
{"title": "Wuthering Heights", "author": "Emily Brontë"},
{"title": "War and Peace ", "author": "Tolstoy"}
]

#define a key
a_key = "title"

list_of_values = [a_dict[a_key] for a_dict in books]

print(list_of_values)


['Pride and Prejudice ', 'David Copperfield', 'Wuthering Heights', 'War and Peace ']

Example: Use map() and lambda

Another way to find the values is by using map() and lambda functions. Lambda takes the key (whose corresponding values is to be found) and name of the iterable as arguments. It returns the corresponding values of the given key to the map() function. list() constructor returns the list of comma-separated values.

#list of dictionaries
books = [
{"title": "Pride and Prejudice ", "author": "Jane Austen"},
{"title": "David Copperfield", "author": "Charles Dickens"},
{"title": "Wuthering Heights", "author": "Emily Brontë"},
{"title": "War and Peace ", "author": "Tolstoy"}
]

list_of_values = list(map(lambda d: d['title'], books))

print(list_of_values)


['Pride and Prejudice ', 'David Copperfield', 'Wuthering Heights', 'War and Peace ']

Example: Use operator and functools Module

This method uses the combination of itertools and the operator module in python to find the values. operator.itemgetter() function stores all the values associated with the given key in a variable. This variable and map are passed as arguments to functools.partial(). It is a higher-order function that takes a function as input like map and binds multiple arguments to the same function. Python 3 uses list() constructor to return a list since map returns an iterator.

#list of dictionary
books = [
{"title": "Pride and Prejudice ", "author": "Jane Austen"},
{"title": "David Copperfield", "author": "Charles Dickens"},
{"title": "Wuthering Heights", "author": "Emily Brontë"},
{"title": "War and Peace ", "author": "Tolstoy"}
]

import operator, functools

get_value = operator.itemgetter('title')
list_of_values = functools.partial(map, get_value)
print(list(list_of_values(books)))


['Pride and Prejudice ', 'David Copperfield', 'Wuthering Heights', 'War and Peace ']

Example: Use an Empty List and append() Function

This method is very simple and uses Brute Force Approach. It takes an empty list to add values one by one because of which the Space Complexity of this program increases. for loop is used to iterate over the elements of the list books and values matching the given key is added to the empty list using append() function.

#list of dictionary
books = [
{"title": "Pride and Prejudice ", "author": "Jane Austen"},
{"title": "David Copperfield", "author": "Charles Dickens"},
{"title": "Wuthering Heights", "author": "Emily Brontë"},
{"title": "War and Peace ", "author": "Tolstoy"}
]

#empty list
list_of_values = ['']
j=0
for i in books:
    if j==0:
        list_of_values[0]=(i['title'])
    else:
        list_of_values.append(i['title'])
    j+=1
print(list_of_values)


['Pride and Prejudice ', 'David Copperfield', 'Wuthering Heights', 'War and Peace ']

Conclusion

In this article, we learned to find the list of values from a given list of dictionaries by using several built-in functions such as append(), map(), lambda, operator and functools module etc and different examples to find the values on the basis of the given key. The best method is the list comprehension technique. You can even use generators in Python to perform this operation.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.