
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get All Values from a Python Dictionary
In this article, we will discuss how to extract a list of all the values from a Python dictionary. Following are various ways to retrieve list of all the values from a Python dictionary -
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.
Using dict.values() & list() Functions
To obtain the list of values from a dictionary, we need to use dictionary.values() in conjunction with the list() function.
The values() method is used to access values from the key-value pairs, and then we need to convert the values to a list using the list() function.
Example
Following is an example -
# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} values = demoDictionary.values() list = list(values) # Printing the list of values of a dictionary using values() function print(list)
When you run the program, it will show this output -
['TutorialsPoint', 'Python', 'Codes']
Using [ ] and "*"
We may get the entire list of dictionary values by using [] and *. The values() method retrieves the values from the dictionary, and "*" is used to get only values, and we then get into a list using the "[ ]" function.
Example
The following program returns the list of all the values of a dictionary using [] and * operators -
# Dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14:'Codes'} values = demoDictionary.values() # Printing the list of values print(*[values])
Output
After running the program, you will get this result -
dict_values(['TutorialsPoint', 'Python', 'Codes'])
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.
Example
The following program returns the list of all the values of a dictionary using the List comprehension method -
# Dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} dict_valuesList = [demoDictionary[dict_key] for dict_key in demoDictionary] # Printing values of a dictionary print(dict_valuesList)
This output will be displayed when the program runs -
['TutorialsPoint', 'Python', 'Codes']
Using append() function & For loop
Here we are using the for loop to traverse over all the values of the dictionary using the values() function. The values() method is used to return a view object. 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, which is used to add the element to the list at the end by passing the corresponding value as an argument to it.
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'} 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)
You will see this result after executing the program -
['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.