You can get or convert 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 e.t.c.
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 as 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 as 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 convert 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 dictionary 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. Iterate dict values using for loop, for every iteration, append the dictionary value to an empty list.
# 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 of List to Lists
Create a dictionary containing a list as its values and apply the values() method, it will return the dict values as a list of lists. Here, your resultant list is a nested list that contains the elements as a list from dictionary values.
# 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()
The map() function takes the list of keys and dictionary.get as input, this method returns all the values as list that are associated with the keys.
# 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() result 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 is used to convert the dictionary values to a list in Python. And also explained using some of the other methods with examples.
Happy learning!!
Related Articles
- Remove multiple keys from Python dictionary
- Python get dictionary keys as a list
- Get all keys from dictionary in Python
- Python sort dictionary by key
- How to sort dictionary by value in Python
- How to create nested dictionary in Python
- Python check if key exists in dictionary
- Python iterate over a dictionary
- Python add keys to dictionary
- How to append Python dictionary to dictionary?
- How to convert Python dictionary to JSON?
- How to add items to a Python dictionary?
- Sort Python dictionary explained with examples
- Python dictionary comprehension
- Dictionary methods
- How to Append Dictionary to List in Python?
- How to check Python Version?
- Python Merge Two Dictionaries into Single