How to get a list of all the values from a Python dictionary?


In this article, we'll show you how to use several ways to extract a list of all the values from a Python dictionary. Using the following methods, we may obtain a list of all the values from a Python dictionary −

  • Using dict.values() & list() functions

  • Using [] and *

  • Using List comprehension

  • Using append() function & For loop

Assume we have taken an example dictionary. We will return the list of all the values from a python dictionary using different methods as specified above.

Method 1: Using dict.values() & list() functions

To obtain the list, we can utilise dictionary.values() in conjunction with the list() function. The values() method is used to access values from the key: value pairs and the values are then converted to a list using the list() function.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary

  • Print the list of all the values of a dictionary with the values() function (the dict.values() method provides a view object that displays a list of all the values in the dictionary in order of insertion) by applying it to the input dictionary and convert the result to a list using the list() function(converts the sequence/iterable to a list).

Example

The following program returns the size of the list using the len() function −

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of values of a dictionary using values() function print(list(demoDictionary.values()))

Output

On executing, the above program will generate the following output −

['TutorialsPoint', 'Python', 'Codes']

Method 2: Using [ ] and *

We may get the entire list of dictionary values by using [] and *. Here, values() is a dictionary method that is used to retrieve the values from the key:value pair in the dictionary, and * is used to get only values rather than dict values, and we then get into a list using the list() function.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary.

  • Print the list of all the values of a dictionary with values() function (A view object is returned by the values() method. The dictionary values are stored in the view object as a list), [], * operator.

Example

The following program returns the list of all the values of a dictionary using [] and * operator

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of values of a dictionary with values() function # and * operator print([*demoDictionary.values()])

Output

On executing, the above program will generate the following output −

['TutorialsPoint', 'Python', 'Codes']

Method 3: Using List comprehension

To find the list of values, this method uses a list comprehension technique. It accepts a key as input and, using a for loop, returns a list containing the corresponding value for each occurrence of the key in each dictionary in the list. It is more elegant and pythonic when compared to others.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary

  • Get all the list of values of the dictionary using the list comprehension by traversing through each value of a dictionary.

demoDictionary[dict_key] represents dictionary value
  • Print the list of all the values of an input dictionary.

Example

The following program returns the list of all the values of a dictionary using the List comprehension method −

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Getting the list of values of the dictionary with the list comprehension # demoDictionary[dict_key] represents dictionary value dict_valuesList = [demoDictionary[dict_key] for dict_key in demoDictionary] # Printing the list of all the values of a dictionary print(dict_valuesList)

Output

On executing, the above program will generate the following output −

['TutorialsPoint', 'Python', 'Codes']

Method 4: Using append() function & For loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary.

  • Create an empty list to store all the keys of an input dictionary.

  • Use the for loop, to traverse through all the values of the dictionary using the values() function (A view object is returned by the values() method. The dictionary values are stored in the view object as a list).

  • Append each value of the dictionary to the list using the append() function(adds the element to the list at the end) by passing the corresponding value as an argument to it.

  • Print the list of all the values in a dictionary.

Example

The following program returns the list of all the values of a dictionary using the append() function & For loop −

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # an empty list for storing dictionary values dictValuesList = [] # Traversing through all the values of the dictionary for dictValue in demoDictionary.values(): # appending each value to the list dictValuesList.append(dictValue) # Printing the list of values of a dictionary print(dictValuesList)

Output

On executing, the above program will generate the following output −

['TutorialsPoint', 'Python', 'Codes']

We used an empty list to store the dictionary's values, then iterated over the values, appended them to the list with the append() function, and displayed them.

Conclusion

This article taught us how to use the values() function to obtain the entire dictionary's values as well as how to use the list() function to turn the values of the dictionary into a list. Additionally, we learned how to use the list comprehension and for loop in the same code to transform the values from the dictionary returned by the values() method to a list. Finally, we learned how to add elements to a list using the append() function (Here we added values to the list).

Updated on: 02-Nov-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements