In this article, we will dicsuss different ways to get the dictionary values as List in Python.
Table Of Contents
Introduction
Suppose we have a dictionary like this,
# A Dictionary containing student details student = { 'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi', 'Country' : 'India'}
It contains a student infromation like Name, Age, City, and Country. We want to create a list of all values in this dictionary. Like this,
['Ritika', 27, 'Delhi', 'India']
There are different ways to do this. Let’s discuss them one by one.
Method 1: Using dict.values()
In Python, the dict class provides a function values(). It returns a view on dictionary values. Then we can pass it to the list constructor, and it will return a list of dictionary values. Let’s see an example,
# A Dictionary containing student details student = { 'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi', 'Country' : 'India'} # Get values of dictionary as List listOfValues = list(student.values()) # print the conetnts of list print(listOfValues) # print the type print(type(listOfValues))
Output:
Frequently Asked:
['Ritika', 27, 'Delhi', 'India'] <class 'list'>
The values() method of dictionary returned all values, and we created a list of all values. We also printed the type of list object, just to make it confirm, that we have a list only.
Method 2: Using For Loop
Create a new empty list. Iterate over all values of dictionary, and add them to new list, one by one. We can iterate over all values of dictionary by applying a for loop over the sequence returned by values() function of dictionary. Let’s see an example,
# A Dictionary containing student details student = { 'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi', 'Country' : 'India'} # Create empty List listOfValues = [] # Get values of dictionary as List for value in student.values(): listOfValues.append(value) # print the conetnts of list print(listOfValues) # print the type print(type(listOfValues))
Output:
['Ritika', 27, 'Delhi', 'India'] <class 'list'>
It gave us a list of all values in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.
Method 3: Using List Comprehension
This solution is similar to the previous one, but with one difference only. Instead of a direct for loop, we will use the List Comprehension, to iterate over all values in dictionary, and appending them to a new list. Let’s see an example,
# A Dictionary containing student details student = { 'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi', 'Country' : 'India'} # Get values of dictionary as List listOfValues = [value for value in student.values()] # print the conetnts of list print(listOfValues) # print the type print(type(listOfValues))
Output:
['Ritika', 27, 'Delhi', 'India'] <class 'list'>
It gave us a list of all values in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.
Method 4: Using Unpacking
We can also unpack a sequence of values, returned by dict.values() function, by applying the astrik to it. Then we can encapsulate this sequence of values, into a list object. Let’s see an example,
# A Dictionary containing student details student = { 'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi', 'Country' : 'India'} # Get values of dictionary as List listOfValues = [*student.values()] # print the conetnts of list print(listOfValues) # print the type print(type(listOfValues))
Output:
['Ritika', 27, 'Delhi', 'India'] <class 'list'>
It gave us a list of all values in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.
Method 5: Using itemgetter
Get a sequence of all key-value pairs of dictionary. Then select the value from each pair, and append to a list.
To implement this, follow these steps,
- Get a sequence of key-value pairs of dictionary using the items() function.
- Pass the sequence of pairs, and itemgetter(1) function to the map() method.
- The map() method will apply the argument itemgetter(1), on each pair in the sequence. Basically for each pair, it selects the value from pair.
- Create a list from values returned by the map() function. It will be the list of dictionary values only.
Let’s see an example,
from operator import itemgetter # A Dictionary containing student details student = { 'Name' : 'Ritika', 'Age' : 27, 'City' : 'Delhi', 'Country' : 'India'} # Get values of dictionary as List listOfValues = list(map(itemgetter(1), student.items())) # print the conetnts of list print(listOfValues) # print the type print(type(listOfValues))
Output:
['Ritika', 27, 'Delhi', 'India'] <class 'list'>
It gave us a list of all values in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.
Summary
We learned about different ways to get dictionary values as list in Python. Thanks.