A dictionary in Python consists of a key-value pair found in the curly braces. The key value pairs are set apart by commas while the key’s value is separated by the colon. Dictionaries do not have a specified order therefore for them to have an order, they can be converted to lists.
Some of the methods by which the Python dictionary values are converted to a list are:
- Using list & items() Method
- Using * operator
- Using keys() Method
- Using values() Method
- Using List Comprehension
- Using map() Function
- Using Zip() Function
- Using for loop
How to Convert a Python Dictionary Value to List?
A Python dictionary value can be converted to the list using dict.values(). A Python object is returned which has the list of the values in the dictionary. Elements in the dictionary are in the form of key-value pairs. The value can have any data type.
We will discuss each method of converting the Python dictionary value to a list in detail:
Method 1: Using list & items() Method
Python Dictionary values can be converted to list by the items() function. This will return a repeatable sequence of key-value pairs. The list() function will return a list of tuples from the repeatable sequence. The following code shows how list and items() method can be used to convert a dictionary value to the list:
Dict = {'Hi': 13, 'Python': 22, 'Tutorial': 15}
res = list(Dict.items())
print(res)
In the above code,
- A variable Dict is created which is stored in an input dictionary.
- Dict.items() function gets the key value pairs from the dictionary Dict.
- list() function converts the dictionary elements to a list of tuples
- print(res) prints the list of the dictionary values.
Output:
Method 2: Using * operator
Another way of converting the dictionary values to a list in Python is by using the * operator. Use * operator around the dict.values method. Store this value in a list. This method will return the dictionary values as a list.
my_dict = {
"language": "Python", "articles": 600, "authors": 11, "duration": "30 days"}
my_list = [*my_dict.values()]
print("The resultant list will be:", my_list)
In the above code,
- firstly define a dictionary my_dict and give it some value.
- Use the * operator around my_dict.values() and store its value in my_list.
- Print the result by using the print() function
Output:
Method 3: Using keys() Method
keys() function in Python converts the dictionary values into a list. The following method shows how keys() method converts the dictionary values to a list:
inp_dict = {'Hi': 10, 'Tutorial': 18, 'Python': 17}
res_list= list(inp_dict.keys())
print(res_list)
In the above code,
- Create a dictionary variable inp_dict and assign some value to it.
- Use the keys() function to get the object that represents all the key value pairs of a dictionary.
- Wrap the key() method applied on the input dictionary with the list() function and store it in rhe variable res_list.
- print(res_list) prints the result
Output:
Method 4: Using values() Method
Python function values() convets the dictionary values to a list. Let us look at the following code to demonstrate the working of keys() function:
inp_dict = {'Hi': 13, 'Tutorial': 22, 'Python': 40}
res_list = list(inp_dict.values())
print(res_list)
In the above code,
- An input dictionary inp_dict is defined.
- inp_dict.values() returns the key-value pairs. Wrap it around the list() function to get the returned value as a list. Store it in a variable res_list
- Print the list using print(res_list)
Output:
Method 5: Using List Comprehension
We can also get the dictionary values to list using the list comprehension. It is a simple and concise method to getthe lists that are iterable. The given code tells how the Python dictionary values are converted to list:
inp_dict = {"apple": 1, "banana": 4, "lychee": 8}
res_list = [(key, value) for key, value in inp_dict.items()]
print(res_list)
In the above code,
- A dictionary with the name inp_dict with three key value pairs is defined and given a value.
- A list comprehension is defined named res_list which takes all the key value pairs from inp_dict
- print(res_list) prints the list containing the dictionary values.
Output:
Method 6: Using map() Function
The map() function converts the Python dictionary values to list. Let us look at the following code to see how map() function converts the dictionary values to list:
dict = {'course':'Web Development','fee':80, 'tutor':'James'}
print("Dictionary:", dict)
keys = ['course', 'fee', 'tutor']
value = list(map(dict.get, keys))
print("List:", value)
In the above code,
- A dictionary item dict is defined and assigned a value.
- print(“Dictionary:”, dict) prints the input dictionary
- keys = [‘course’, ‘fee’, ‘tutor’] defines a list and keys are given as input values
- The map() function is used with dict.get to get the values corresponding to each key. The map function returns a map object which is converted to a list ‘value’ using list() function.
- print(“List:”, value) prints the list that contains all the values corrresponding to the keys.
Output:
Method 7: Using Zip() Function
The zip() function changes the dictionary values to the list. The following code demonstrates the working of zip() function:
inp_dict = {'course':'Web Development','fee':80, 'tutor':'James'}
res_list = zip(inp_dict.keys(), inp_dict.values())
res_list = list(res_list)
print(res_list)
In the above code,
- An input dictionary inp_dict with three key value pairs is defined
- zip() function is wrapped around the values taken from the key value pairs of the input dictionary.
- The returned zip object is stored in a list named res_list
- print(res_list) prints the resulting list that contains both the keys and values from the input dictionary.
Output:
Method 8: Using For Loop
Lastly, for loop can be used to convert the Python values to list. Let us see the following code to see how the for loop can convert the dictionary values to list:
my_dict = {'course':'Web Development','fee':80, 'tutor':'James'}
value_list = []
for i in my_dict.values():
value_list.append(i)
print("List:", value_list)
In the above code,
- The input dictionary my_dict has three key value pairs
- An empy list value_list is defined which will have the values from the input dictionary
- In the for loop, the value i is appended to the value_list using append() function
- print(“List:”, value_list) prints the value_list, the list in which the dictionary values are stored
Output:
Conclusion
In this article, we have discussed how you can convert the Python dictionary values into a list through different methods including dict.values(). The data can be mapped in different ways using dictionaries. The code for extracting the values from the key-value pairs and converting them to a list is simple. In this article, each method of converting a Python dictionary value to a list was discussed and illustrated with an example.