- How to convert Python Dictionary to a list?
- Using the list & items() Method
- Algorithm (Steps)
- Example
- Output
- Using keys() Method
- Algorithm (Steps)
- Example
- Output
- Using values() Method
- Example
- Output
- Using List comprehension
- Example
- Output
- Using Zip() Function
- Example
- Output
- Using map() Function
- Example
- Output
- Using for loop & items() Method
- Algorithm (Steps)
- Example
- Output
- Conclusion
- How to Extract Dictionary Values as a List
- Examples of Extracting Dictionary Values as a List in Python
- Example 1: Use a list() function to extract dictionary values as a list
- Example 2: Use a list comprehension to extract dictionary values
- Example 3: Use a for loop to extract dictionary values
- Extract the Dictionary Values as a List of Lists
How to convert Python Dictionary to a list?
In this article, we will show you how to convert a python dictionary to a list. Below are the methods to accomplish this task:
- Using list & items() Method
- Using keys() Method
- Using values() Method
- Using List Comprehension
- Using Zip() Function
- Using map() Function
- Using for loop & items() Method
Dictionaries are Python’s version of an associative array data structure. A dictionary is a collection of key-value pairs. Each key pair is represented by a key pair and its associated value.
A dictionary is defined by a list of key-value pairs enclosed in curly braces and separated by commas. Each key’s value is separated by a colon(:).
A dictionary cannot be sorted solely for the purpose of obtaining a representation of the sorted dictionary. Dictionaries are orderless by definition, while other kinds, such as lists and tuples, are not. As a result, you require an ordered data type, which is a list—most likely a list of tuples.
Python’s dictionary class has three methods for this purpose. The methods items(), keys(), and values() return view objects comprising of tuple of key-value pairs, keys only and values only respectively. The in-built list method converts these view objects in list objects.
Using the list & items() Method
Python’s dictionary class offers the items() function, which returns an iterable sequence of all key-value pairs in the dictionary (dict items). This retuned sequence represents the actual key-value pairs in the dictionary. We can use the list() function to get a list of tuples from this iterable sequence.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
- Create a variable to store an input dictionary.
- Get all the key-value pairs of a dictionary using the items()> function(returns a group of the key-value pairs in the dictionary) and convert the dictionary items(key-value pair) to a list of tuples using the list() function(returns a list of an iteratable).
- Print the resultant list of a dictionary after converting.
Example
The following program converts python dictionary to a list using list & items() Method −
# input dictionary inputDictionary = 'Hello': 10, 'Tutorialspoint': 20, 'python': 30> # converting input dictionary items(key-value pair) # to a list of tuples resultList = list(inputDictionary.items()) # printing the resultant list of a dictionary print(resultList)
Output
On executing, the above program will generate the following output −
[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]
Using keys() Method
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
- Create a variable to store the input dictionary.
- Print the list of all the keys of a dictionary with the keys() function(the dict. keys() method provides a view object that displays a list of all the keys in the dictionary in order of insertion) by applying it to the input dictionary and convert the result to a list using the list() function(converts the sequence/iterable to a list).
Example
The following program returns the list of all the keys of a dictionary using the list() and keys() functions −
# input dictionary inputDictionary = 'Hello': 10, 'Tutorialspoint': 20, 'python': 30> # converting input dictionary keys to a list resultList = list(inputDictionary.keys()) # printing the resultant list of a dictionary keys print(resultList)
Output
On executing, the above program will generate the following output −
['Hello', 'Tutorialspoint', 'python']
Using values() Method
To obtain a list of values, use the values() method of dictionaries. We can convert values into a list by using the list() function.
Example
The following program returns the list of all the values of a dictionary using the list() and values() functions −
# input dictionary inputDictionary = 'Hello': 10, 'Tutorialspoint': 20, 'python': 30> # converting input dictionary values to a list resultList = list(inputDictionary.values()) # printing the resultant list of a dictionary values print(resultList)
Output
On executing, the above program will generate the following output −
Using List comprehension
List comprehension makes it simple to create a list in Python. Furthermore, you can change the value of elements on the same line.
Using dictionary.items() to retrieve the keys and values of the items before adding them to the list.
Example
The following program converts python dictionary to a list using list comprehension −
# input dictionary inputDictionary = 'Hello': 10, 'Tutorialspoint': 20, 'python': 30> # Storing key and value as list of tuples using list comprehension resultList = [(key, value) for key, value in inputDictionary.items()] # printing the resultant list of a dictionary key-values print(resultList)
Output
On executing, the above program will generate the following output −
[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]
Using Zip() Function
The zip() function can be used to combine two lists/iterators. In this manner, we can combine the dictionary’s .keys() and.values() methods to access both values at the same time.
Example
The following program converts python dictionary to a list using list zip() function −
# input dictionary inputDictionary = 'Hello': 10, 'Tutorialspoint': 20, 'python': 30> #combining keys and values of dictionary using zip() function resultList = zip(inputDictionary.keys(), inputDictionary.values()) # converting zip object to list resultList = list(resultList) # printing the resultant list of a dictionary key-values print(resultList)
Output
On executing, the above program will generate the following output −
[('Hello', 10), ('Tutorialspoint', 20), ('python', 30)]
Using map() Function
map() is a built-in function that allows you to map any function over an iterator/list. To create lists of lists, map the list() function over all the elements of dictionary.items().
Example
The following program converts python dictionary to a list using the list map() function −
# input dictionary inputDictionary = 'Hello': 10, 'Tutorialspoint': 20, 'python': 30> # conveting list of keys and values of dictionary to a list using map() function resultList = new_list = list(map(list, inputDictionary.items())) # printing the resultant list of a dictionary key-values print(resultList)
Output
On executing, the above program will generate the following output −
[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]
Using for loop & items() Method
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
- Create a variable to store the input dictionary.
- Creating an empty list that gives the resultant list of a dictionary key, values.
- Use the for loop to traverse through each key value pair of a dictionary using items() function(returns a group of the key-value pairs in the dictionary).
- Use the append() function( adds the element to the list at the end) to append list of corresponding key-value pair to a list.
- Print the resultant list of a dictionary key-values
Example
The following program converts python dictionary to a list using for loop, append(), items() functions −
# input dictionary inputDictionary = 'Hello': 10, 'Tutorialspoint': 20, 'python': 30> # creating an empty list resultList = [] # traversing through each key value pair of a dictionary using items() function for key, val in inputDictionary.items(): # appending list of corresponding key-value pair to a list resultList.append([key, val]) # printing the resultant list of a dictionary key-values print(resultList)
Output
On executing, the above program will generate the following output −
[['Hello', 10], ['Tutorialspoint', 20], ['python', 30]]
Conclusion
In this article, we learned how to use the seven different methods to convert a given dictionary to a list. We also learned how to use the items() function to iterate through the dictionary.
How to Extract Dictionary Values as a List
Here are 3 approaches to extract dictionary values as a list in Python:
(1) Using a list() function:
my_list = list(my_dict.values())
(2) Using a List Comprehension:
my_list = [i for i in my_dict.values()]
(3) Using For Loop:
my_list = [] for i in my_dict.values(): my_list.append(i)
Next, you’ll see few examples of extracting dictionary values as a list.
Examples of Extracting Dictionary Values as a List in Python
Example 1: Use a list() function to extract dictionary values as a list
To start with a simple example, let’s create the dictionary below:
my_dict = print(my_dict) print(type(my_dict))
You’ll then get the following dictionary:
Note that the syntax of print(type(my_dict)) was added at the bottom of the syntax to demonstrate that we got a dictionary.
Now, let’s use a list() function to extract the dictionary values as a list:
my_dict = my_list = list(my_dict.values()) print(my_list) print(type(my_list))
As you can see, the dictionary values were extracted as a list:
Example 2: Use a list comprehension to extract dictionary values
Now, let’s extract the dictionary values as a list using a list comprehension:
my_dict = my_list = [i for i in my_dict.values()] print(my_list) print(type(my_list))
You’ll get the same list as in the previous example:
Example 3: Use a for loop to extract dictionary values
Finally, you may use a For Loop to extract the values as a list:
my_dict = my_list = [] for i in my_dict.values(): my_list.append(i) print(my_list) print(type(my_list))
As before, you’ll get the same list:
Extract the Dictionary Values as a List of Lists
What if you’d like to extract the dictionary values as a list of lists?
For example, suppose that you have the following dictionary:
my_dict = print(my_dict) print(type(my_dict))
You can then apply the following syntax to extract the dictionary values as a list of lists:
my_dict = my_list = list(my_dict.values()) print(my_list) print(type(my_list))
You’ll now get a list of lists:
[[300, 305, 310], [150, 155, 160], [500, 505, 510]]
You may also want to check the following guide that explains how to extract dictionary keys as a list.