In this Python tutorial, we will understand how to get all values from a dictionary in Python.
We can get all values from a dictionary in Python using the following 5 methods.
- Using dict.values()
- Using * and dict.values()
- Using for loop & append()
- Using the map() function
- Using list comprehension & dict.values()
Get all values from a dictionary Python
Here we will illustrate multiple methods to get all values from a Python Dictionary. And for this, we will start with the dict.values() method.
Method 1: Using dict.values()
The dict.values() in Python is a Dictionary method that fetched all the values from the dictionary and returns a dict_values object. This object contains all the values of a dictionary.
However, we can easily convert this dict_values object into a list using the list() constructor in Python.
Here is an example of this approach in Python.
# Defining a dictionary
Countries = {
6:'Canada',
2:'United Kingdom',
1:'United States',
9:'Australia',
7:'China'
}
# Fetching all dictionary values
country_names = list(Countries.values())
# Printing all dictionary values
print(country_names)
In the example, we are fetching all the values from the Countries dictionary using the dict.values() method. After this, convert the result of the dict.values() method into a list using list() function.
The result of the Python program is shown below.
['Canada', 'United Kingdom', 'United States', 'Australia', 'China']
Read: Python Dictionary Copy
Method 2: Using * and dict.values()
Alternatively, we can also use the * operator with the dict.values() method to fetch all the values from a dictionary in Python.
Here is an example of this execution in python.
# Defining a dictionary in Python
user_data = {
'Name': 'Alex',
'Age': 32,
'City': 'Chicago',
'Country': 'United States',
'Technical Skills': ['SQL', 'Java']
}
# Fetching all dictionary values
user_values = [*user_data.values()]
# Printing all dictionary values
print(user_values)
In this example, we utilized the * operator with dict.values() to fetch all the values from the user_data dictionary.
['Alex', 32, 'Chicago', 'United States', ['SQL', 'Java']]
Read: Python dictionary contains
Method 3: Using for loop & append()
In this method, we will use the for loop to iterate over each dictionary value using dict.values(). And then we use the append() method to store the value in a Python List.
The execution related to this approach is given in the Python code below.
# Defining a dictionary
Countries = {
6:'Canada',
2:'United Kingdom',
1:'United States',
9:'Australia',
7:'China'
}
# Defining empty dictionary
country_names = []
# Fetching all dictionary values
for value in Countries.values():
country_names.append(value)
# Printing all dictionary values
print(country_names)
- In this example, we used for loop over the Countries.values() and fetch each dictionary value.
- After this, we used the append() method on the country_names list to store every dictionary value in it.
The result of the Python program is given below.
['Canada', 'United Kingdom', 'United States', 'Australia', 'China']
Read: Python dictionary length
Method 4: Using list comprehension & dict.values()
- In this method, we used the same dict.values() method to fetch all the dictionary values as dict_values object.
- Then we used the concept of list comprehension to generate a new list using the result of dict.values().
# Defining a dictionary in Python
countries_hdi = {
'Canada': 0.937,
'United Kingdom': 0.935,
'United States': 0.921,
}
# Fetching all dictionary values
hdi_values = [value for value in countries_hdi.values()]
# Printing all dictionary values
print(hdi_values)
Here we used the countries_hdi.values() method to get all list of values from countries_hdi dictionary. And we also used list comprehension using the countries_hdi.values() method.
This will result in forming another list named hdi_values containing all the values from the countries_hdi dictionary values. Once we print the hdi_values list, we will get the following result.
[0.937, 0.935, 0.921]
Read: Python Dictionary Sort
Method 5: Using the map() function
Another way to get all values from a Python Dictionary is by using the map() function with dict.get() method.
The dict.get() function in Python allows fetching the value of a particular key given in a dictionary. On the other hand, the map() function allows to execution of a function for each value given in an iterable.
So, in this approach, we will fetch all the keys from a dictionary and use each key name with map() and dict.get() function. This allows fetching each value corresponding to a key in Dictionary.
An example of this approach is shown in the code below.
# Defining a dictionary in Python
user_data = {
'Name': 'Alex',
'Age': 32,
'City': 'Chicago',
'Country': 'United States',
'Technical Skills': ['SQL', 'Java']
}
# Fetching all dictionary values
keys = list(user_data.keys())
user_values = list(map(user_data.get, keys))
# Printing all dictionary values
print(user_values)
Once we execute the above Python program, we will get the following result.
You may also like to read the following Python tutorials.
Conclusion
So, in this Python tutorial, we understood how to fetch or get all values from a dictionary Python. Additionary, we have covered 5 different methods for this task and each method covers an example.
The 5 ways that we covered are given below.
- Get all values from a dictionary Python using dict.values()
- Get all values from a dictionary Python using * and dict.values()
- Get all values from a dictionary Python using for loop & append()
- Get all values from a dictionary Python using the map() function
- Get all values from a dictionary Python using list comprehension & dict.values()
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.