Get Dictionary values as List in Python

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.

Advertisements

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:

['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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top