In this tutorial, we are going to discuss the top five ways to convert a Python dictionary to a Python list. So, let’s get started!
5 Methods to Convert a Dictionary to a list
Let’s now learn how you can convert a dictionary to a list in Python. In the next few sections, you’ll see various methods and functions that are used in Python to change the datatype from dictionary to list. Let’s get started!
Method 1: Using dict.items() function
In this method, we will be using the dict.items() function of dictionary class to convert a dictionary to a list. The dict.items()
function is used to iterate over the key: value pairs of the Python dictionary. By default, the key: value pairs are stored in the form of a Python tuple in the iterator returned by the dict.items()
function. We can pass this returned iterator to the list()
function which will finally give us a Python list of tuples containing the key: value pairs of the given Python dictionary. Let’s get into the actual Python code to implement this method.
# Method- 1
# Create a Python dictionary
dr = {'AskPython': 'Python', 'LinuxForDevices': 'Linux', 'QuickExcel': 'Excel'}
print('Given Python dictionary:\n')
print(dr)
# Convert the above Python dictionary to a Python list
# Using the dict.items() function
ls = list(dr.items())
print('\nThis is the converted list:\n')
print(ls)
Output:
Given Python dictionary:
{'AskPython': 'Python', 'LinuxForDevices': 'Linux', 'QuickExcel': 'Excel'}
This is the converted list:
[('AskPython', 'Python'), ('LinuxForDevices', 'Linux'), ('QuickExcel', 'Excel')]
Method 2: Using zip() function
In this method, we will use the zip() function in Python to convert a dictionary to a list. The zip()
function in Python is used to combine two iterator objects by zipping their values.
Here we will pass over two Python lists as the two iterator objects, one will be the list of all the keys of the given Python dictionary and the other will be the list of all the values of the given Python dictionary. We will get these two lists using the dict.keys()
and the dict.values()
functions of the Python dictionary class. By default, the key: value pairs are stored in the form of a Python tuple in the iterator returned by the zip()
function. We can pass this returned iterator to the list()
function which will finally return a Python list of tuples containing all the key: value pairs of the given Python dictionary. Let’s see how to implement this method through Python code.
# Method- 2
# Create a Python dictionary
dr = {'Python': '.py', 'C++': '.cpp', 'Csharp': '.cs'}
print('Given Python dictionary:\n')
print(dr)
# Convert the above Python dictionary to a Python list
# Using the zip() function
iter = zip(dr.keys(), dr.values())
ls = list(iter)
print('\nThis is the Converted list:\n')
print(ls)
Output:
Given Python dictionary:
{'Python': '.py', 'C++': '.cpp', 'Csharp': '.cs'}
This is the Converted list:
[('Python', '.py'), ('C++', '.cpp'), ('Csharp', '.cs')]
Method 3: Using map() function
In this method, we will use the map() function in Python to convert a dictionary to a list in Python. The map()
function in Python is used to map any Python function over an iterator object.
Here we will pass the Python list()
function as the first argument, and the iterator object returned by the dict.item()
function as the second argument. Then we can pass the iterator returned by the map()
function to the list()
function which will return a Python list of lists containing all the key: value pairs of the given Python dictionary. Let’s write the Python code to implement this method.
# Method- 3
# Create a Python dictionary
dr = {'Amazon': 'AWS', 'Microsoft': 'AZURE', 'Google': 'GCP'}
print('Given Python dictionary:\n')
print(dr)
# Convert the above Python dictionary to a Python list
# Using the map() function
iter = map(list, dr.items())
ls = list(iter)
print('\nThis is the Converted list:\n')
print(ls)
Output:
Given Python dictionary:
{'Amazon': 'AWS', 'Microsoft': 'AZURE', 'Google': 'GCP'}
This is the Converted list:
[['Amazon', 'AWS'], ['Microsoft', 'AZURE'], ['Google', 'GCP']]
Method 4: Using simple for loop iteration
In this method, we will use a simple for loop to convert a dictionary to a list. We will first create an empty list, then iterate over the keys of the given Python dictionary using the for loop. Finally, we will create one Python tuple for each key: value pair of the given Python dictionary as we iterate over it and append this tuple to the empty list, we created. Let’s implement this method through Python code.
# Method- 4
# Create a Python dictionary
dr = {'Sanjay': 'ECE', 'Abhishek': 'EE', 'Sarthak': 'ICE'}
print('Given Python dictionary:\n')
print(dr)
# Create an empty Python list
ls = []
# Convert the above Python dictionary to a Python list
# Using the for loop iteration
for key in dr:
item = (key, dr[key])
ls.append(item)
print('\nThis is the Converted list:\n')
print(ls)
Output:
Given Python dictionary:
{'Sanjay': 'ECE', 'Abhishek': 'EE', 'Sarthak': 'ICE'}
This is the Converted list:
[('Sanjay', 'ECE'), ('Abhishek', 'EE'), ('Sarthak', 'ICE')]
Method 5: Using list comprehension
The list comprehension method is the most widely used and compact way to initialize a Python list. We can also provide the list elements in the same single line using a for loop. Here we will fetch the key: value pairs of the given Python dictionary using a for loop and dict.items()
function. Then we will initialize the Python list with the fetched key: value pairs of the given Python dictionary. Let’s see how we can use this method to convert a given Python dictionary to a Python list.
# Method- 4
# Create a Python dictionary
dr = {'A': 65, 'B': 66, 'C': 67, 'D': 68}
print('Given Python dictionary:\n')
print(dr)
# Convert the above Python dictionary to a Python list
# Using the list comprehension
ls = [(key, value) for key, value in dr.items()]
print('\nThis is the Converted list:\n')
print(ls)
Output:
Given Python dictionary:
{'A': 65, 'B': 66, 'C': 67, 'D': 68}
This is the Converted list:
[('A', 65), ('B', 66), ('C', 67), ('D', 68)]
Summing-up
In this tutorial, we have learned the top five ways to convert a dictionary to a list in Python. Hope you have understood the things and ready to experiment with these methods. Thanks for reading this article and stay tuned with us for more such exciting learning content for Python programming.