How To Get Values As A List From Dictionary In Python – Definitive Guide

Python dictionary allows you to store values as key-value pairs.

You can get values as a list from Dictionary using the list(yourdict.values()) method in Python.

This tutorial teaches you the different methods to get values as a list from a dictionary in Python.

To get the keys as a list, read How to Return Dictionary Keys as a List in Python

Using List() function

This section teaches you how to use the list() constructor and the dict.values() method to get values as a list from a dictionary.

  • The dict.values() method returns a view of the values available in the dictionary. It shall contain duplicate values as well.
  • The list() constructor converts the values view to a list.

Code

The code below demonstrates how to get values as a list from the dictionary using the values() method and the list() constructor.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}


list(yourdict.values())

Output

All the values in the dictionary are added to the list.

    [1, 2, 3, 4]

Unpack with *

You can also unpack the dictionary values as a list using the * operator. This functionality was proposed in the PEP-448.

This is supported from Python version 3.5. So you can use this method if you’re using Python version 3.5 or a later version.

Checking Python Version

To check which version of Python you are using, use the below code snippet.

Code*

import sys
print(sys.version)

Output

3.8.2 (default, Sep  4 2020, 00:03:40) [MSC v.1916 32 bit (Intel)]

Now you know which version of Python you are using. If it is equal to or greater than 3.5, you can use the unpack * operator.

Getting Values as List using * operator

You need to prefix the * operator with the dictionary values() method and pass it to the [] square brackets. You’ll see the values extracted as a list.

Code

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

[*yourdict.values()]

Output

    [1, 2, 3, 4]

This is how you can extract dictionary values as a list in Python.

Using List Comprehension

This section teaches you how to use list comprehension to get values as a list from Dictionary in Python.

List comprehension provides a simpler syntax to iterate over an iterable and create a new list object.

You can use this method when you want to create a list with specific values from a dictionary.

Code

The below code demonstrates how to use list comprehension to create a list based on all values available from the list.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

list_of_values = [val for val in yourdict.values()]

list_of_values

Output

    [1, 2, 3, 4]

Using For Loop

This section teaches you how to use the simple for loop to iterate over dictionary values and add the items to a list.

  • You can iterate over the values using the for loop with the dict.values() method.
  • During each iteration, you can append the values to the list.

Code

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

your_list = []

for val in yourdict.values():
    your_list.append(val)

your_list

Output

All the values in the dictionary are added to a new list(Including duplicates).

    [1, 2, 3, 4]

Get Values As a List Sorted By Keys

To get the values as a list sorted based on its keys, you need to iterate over the sorted dictionary keys using the sorted() method.

During each iteration, fetch the value of the key and add the value to a new list.

Code

The code below demonstrates how to get values as a list sorted based on its keys.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

your_list = []

for key in sorted(yourdict.keys()):

    your_list.append(yourdict[key])

your_list

Output

The values are added to the list based on the order of the sorted keys.

    [4, 1, 3, 2]

Get Values As List Sorted By Values

To get the values as a list sorted based on its values, you need to iterate over the sorted dictionary values using the sorted() method.

During each iteration, you can add the value directly to the list.

Code

The code below demonstrates how to get values as a list sorted based on the values themselves.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

your_list = []

for val in sorted(yourdict.values()):

    your_list.append(val)

your_list

Output

The values are added to the list based on the sorted values.

    [1, 2, 3, 4]

Get Specific Values As List

You can use list comprehension to get only specific values from a dictionary as a list.

The list comprehension checks if each value is passing your desired condition. Only the passed values are added to the newly created list.

Code

The code below demonstrates how to use list comprehension to get only specific values from the dictionary as a list.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

value_to_check = 2

values_list = [val for val in yourdict.values() if val > value_to_check]

values_list

Output

    [3, 4]

This is how you can get specific values from the dictionary as a list.

Additional Resources

Leave a Comment