You can get dictionary values as a list using dict.values() method in Python, this method returns an object that contains a list of all values stored in the dictionary. Elements in the dictionary are in the form of key-value pairs and each key has its corresponding value. A value can be any data type such as a number(int, float), a string, a list, a tuple, or even another dictionary.
In this article, I will explain how to convert dictionary values to a list using dict.values() method and some more methods of Python with examples.
1. Quick Examples of Getting Dictionary Values to List
Following are quick examples of how to get dictionary values to a list.
# Below are the quick examples
# of converting dictionary values to a list.
# Example 1: Get the dict values as a list
# using values() method.
list = list(dict.values())
# Example 2: Get the dict values as a list
# using list comprehension.
list = [i for i in dict.values()]
# Example 3: Create empty list
list = []
# Get the list of dict values using for loop
for i in dict.values():
list.append(i)
# Example 4: Get the dict values as a list of lists
# using dict.values()
list = list(dict.values())
# Example 5: Get the dict values as list
# using * operator
print([*dict.values()])
# Example 6: Get list of dict values using map
value = list(map(dict.get, keys))
# Example 7: Get list of dict values using items()
list = []
for key, value in dict.items(my_dict):
list.append(value)
A Python dictionary is a collection that is unordered, mutable, and does not allow duplicates for keys. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys.
Let’s create a dictionary and run the above examples.
# Create dictionary
dict = {‘course’:’python’,’fee’:4000, ‘tutor’:’Richerd’, ‘duration’:’45 days’}
print(“Dictionary:”, dict)
print(type(dict))
Yields below output.
2. Get Python Dictionary values to List
You can use the values() method of a dictionary to get a dict_values object which is an iterated object, and pass this object to the list() constructor to get a list object with dictionary values as elements.
# Get the dict values as a list
# using values() method.
list = list(dict.values())
print(“List”, list)
print(type(list))
Yields below output. method.
3. Convert Dictionary values to List using List Comprehension
We can also get the Python list from dict values by using list comprehension. It is a concise and elegant way to create lists using an iterable. Let’s apply list comprehension to a given dict and get its values as a list.
# Get the dict values as a list
# using list comprehension.
list = [i for i in dict.values()]
print(“List:”, list)
print(type(list))
Yields below output.
# Output:
# List: [‘python’, 4000, ‘Richerd’, ’45 days’]
# <class ‘list’>
4. Convert Python Dictionary values to List Using For Loop
By using for loop we can manually convert the Python dictionary values to the list. To iterate dict values use for loop, for every iteration append the dict value to an empty list. Let’s iterate the given dict and get the list of dict values.
# Create empty list
list = []
# Get the list of dict values using for loop
for i in dict.values():
list.append(i)
print(“List:”, list)
Yields below output.
# Output:
# List: [‘python’, 4000, ‘Richerd’, ’45 days’]
# <class ‘list’>
5. Convert Dictionary Values to List of Lists
Create a dictionary containing a list of values as its values and apply the values() method, it will return the list of dict values. Finally, we can get the list of lists of dict values using the list() method.
#Let’s create list of values of dict
dict = {‘course’: [‘Python’, ‘Pandas’, ‘Java’] ,’fee’:[4000, 2500, 4500], ‘duration’: [’45 days’, ’30 days’, ’50 days’]}
print(“Dictionary:”, dict)
print(type(dict))
# Get the dict values as a list of lists
# using dict.values()
list = list(dict.values())
print(“List:”, list)
print(type(list))
Yields below output.
# Output:
# Dictionary: {‘course’: [‘Python’, ‘Pandas’, ‘Java’], ‘fee’: [4000, 2500, 4500], ‘duration’: [’45 days’, ’30 days’, ’50
# days’]}
# <class ‘dict’>
# List: [[‘Python’, ‘Pandas’, ‘Java’], [4000, 2500, 4500], [’45 days’, ’30 days’, ’50 days’]]
# <class ‘list’>
6. Convert Dictionary Values to List using * Operator
Using the * operator we can also convert all dictionary values to a list in Python. Apply * operator in front of dict.values() method and pass them into the list. This syntax will return all the dict values as a list.
# Get the dict values as list
# using * operator
print(“List:”, [*dict.values()])
# Output:
# List: [‘python’, 4000, ‘Richerd’, ’45 days’]
7. Convert Dictionary Values to List using map()
Get the dict values as a list using map() function. Let’s pass the dict.get and keys as parameters to the map() method and get the dict values. Finally, use list() method to convert to a list.
# Create dictionary
dict = {‘course’:’python’,’fee’:4000, ‘tutor’:’Richerd’}
print(“Dictionary:”, dict)
keys = [‘course’, ‘fee’, ‘tutor’]
# Get list of dict values using map
value = list(map(dict.get, keys))
print(“List:”, value)
# Output:
# Dictionary: {‘course’: ‘python’, ‘fee’: 4000, ‘tutor’: ‘Richerd’}
# List: [‘python’, 4000, ‘Richerd’]
8. Convert Dictionary Values to List using items() Method
Alternatively, we can get the list of dictionary values using the items() method, which is a dictionary method that returns a tuple of key/value pairs. Iterate the items() method and get the key/value pair. Finally, append each value of the dictionary to an empty list.
# Get list of dict values using items()
list = []
for key, value in dict.items(my_dict):
list.append(value)
print(“List:, list)
# Output:
# List: [‘python’, 4000, ‘Richerd’, ’45 days’]
9. Conclusion
In this article, I have explained dict.values() method, by using this how we can convert the dictionary values to a list in Python. And also explained using some of the python methods how we can get the list of dict values as a list.
Happy learning!!
You can get dictionary values as a list using dict.values() method in Python, this method returns an object that contains a list of all values stored in the dictionary. Elements in the dictionary are in the form of key-value pairs and each key has its corresponding value. A value can be any data type such as Read More Python, Python Tutorial, Python Dictionary Examples