Python list from dictionary values

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 =

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,

There are different ways to do this. Let’s discuss them one by one.

Frequently Asked:

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))

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))

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.

Читайте также:  Html left margin list

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))

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))

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.
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))

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.

Читайте также:  Таблицы истинности логических выражений python

Summary

We learned about different ways to get dictionary values as list in Python. Thanks.

Share your love

Leave a Comment Cancel Reply

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

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

Читайте также:  Html css generate pdf

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

How to Extract Dictionary Values as a List

Data to Fish

Here are 3 approaches to extract dictionary values as a list in Python:

(1) Using a list() function:

my_list = list(my_dict.values())

(2) Using a List Comprehension:

my_list = [i for i in my_dict.values()]

(3) Using For Loop:

my_list = [] for i in my_dict.values(): my_list.append(i)

Next, you’ll see few examples of extracting dictionary values as a list.

Examples of Extracting Dictionary Values as a List in Python

Example 1: Use a list() function to extract dictionary values as a list

To start with a simple example, let’s create the dictionary below:

my_dict = print(my_dict) print(type(my_dict))

You’ll then get the following dictionary:

Note that the syntax of print(type(my_dict)) was added at the bottom of the syntax to demonstrate that we got a dictionary.

Now, let’s use a list() function to extract the dictionary values as a list:

my_dict = my_list = list(my_dict.values()) print(my_list) print(type(my_list))

As you can see, the dictionary values were extracted as a list:

Example 2: Use a list comprehension to extract dictionary values

Now, let’s extract the dictionary values as a list using a list comprehension:

my_dict = my_list = [i for i in my_dict.values()] print(my_list) print(type(my_list))

You’ll get the same list as in the previous example:

Example 3: Use a for loop to extract dictionary values

Finally, you may use a For Loop to extract the values as a list:

my_dict = my_list = [] for i in my_dict.values(): my_list.append(i) print(my_list) print(type(my_list))

As before, you’ll get the same list:

Extract the Dictionary Values as a List of Lists

What if you’d like to extract the dictionary values as a list of lists?

For example, suppose that you have the following dictionary:

my_dict = print(my_dict) print(type(my_dict))

You can then apply the following syntax to extract the dictionary values as a list of lists:

my_dict = my_list = list(my_dict.values()) print(my_list) print(type(my_list))

You’ll now get a list of lists:

[[300, 305, 310], [150, 155, 160], [500, 505, 510]]

You may also want to check the following guide that explains how to extract dictionary keys as a list.

Источник

Оцените статью