In this Python tutorial, we will understand how Python convert dictionary to list using different methods with examples.
To convert a Python dictionary to a list, we can use nine different methods such as items(), values(), or keys() to extract specific components and wrap them in a list constructor. We can use zip() to combine multiple dictionary components, employ list comprehensions for concise transformations, utilize map() for applying a function to each item, iterate over the dictionary with a for loop or for loop with items(), and create structured lists using collections.namedtuple().
Python convert dictionary to list
In Python, we can convert a dictionary to a list using the following 9 methods.
- Using items() Method
- Using values() Method
- Using Zip Function
- Using keys() Method
- Using List Comprehension Method
- Using map() function
- Using loop + items() Method
- Using Iteration Method
- Using Collections.namedtuple() function
Here we will cover all 9 methods to convert dictionary to list in Python with various examples.
1. dict to list Python using items() method
In this section, we will understand how Python convert dictionary to list using the items() method.
The dict.items() method is used to iterate the elements of the dictionary. Using list() we will get all the items converted into the list.
Example:
my_dict ={"b":20,"c":10}
con_lis = list(my_dict.items())
print("Converted dict to list:",con_lis)
- In the above code first, we will create a dictionary by using curly brackets and assign them key-values pairs.
- The con_lis variable needs to be converted into a list so that we can use the list() function and then store the result in the con_lis variable.
Output: This is how Python convert dictionary to list by using items() function.
Converted dict to list: [('b', 20), ('c', 10)]
The screenshot below showcases the output generated by running the code within the PyCharm editor.
2. Python dict to list using values() method
The dict.values() method in Python can easily get a list of values and to convert values to a list, we can use the list() function
Example: Let’s take an example and check how to convert a dictionary to a list
to_dict ={"d":50,"f":10,"g":40}
new_val = list(to_dict.values())
print("Converted dict to list:",new_val)
Output:
Converted dict to list: [50, 10, 40]
Below is an image displaying the results of the code execution in the PyCharm environment:
3. Python Convert Dictionary to List using zip() function
In this example, we can combine the zip(), keys(), and values() methods to convert a dictionary into a list.
To create a list of tuples from a Python Dictionary, we can use the zip() function. The zip() function is mostly used in Python for combining two iterators.
Example: Here is the sample code in Python convert dictionary to list.
to_dict ={"i":160,"w":110,"s":340}
m = list(zip(to_dict.keys(), to_dict.values()))
print("Convert dictionary to list:",m)
Output: Here is the implementation of the following code:
Convert dictionary to list: [('i', 160), ('w', 110), ('s', 340)]
The ensuing image captures the results produced by executing the code in PyCharm.
4. Convert dict to list Python using keys() method
The keys() method in Python is used to get the list of keys from the dictionary. To get the list of all keys, we can combine the list() function with the keys() method.
Let us look at an example of using methods in Python.
new_dict ={"z":320,"p":430,"o":245}
n_val = list(new_dict.keys())
print("Convert dictionary to list",n_val)
Output:
Convert dictionary to list ['z', 'p', 'o']
Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.
5. Convert dictionary to list Python using list comprehension method
In this method, we will dict.items() method to collect the keys and values from a dictionary. After this, we will use the list comprehension method to form a list out of it.
Here is an example of this implementation in Python convert dictionary to list.
n_dictionary ={"James":240,"William":180,"Chris":140}
n_lis = [(key, value) for key, value in n_dictionary.items()]
print(n_lis)
Output:
[('James', 240), ('William', 180), ('Chris', 140)]
After implementing the code in the Pycharm editor, the screenshot is mentioned below.
6. How to convert dictionary to list in Python using map() function
The map function will take a function and a sequence as a parameter and it applies that function to the iterable and returns a new list.
In this example, you can combine the map() and list() functions to convert the dictionary into a list.
Source code:
my_dict ={"Newzealand":860,"Swizerland":780,"Moscow":340}
con_val= list(map(list,my_dict.items()))
print(con_val)
Output:
[['Newzealand', 860], ['Swizerland', 780], ['Moscow', 340]]
A screenshot is mentioned below, after implementing the code in the Pycharm editor.
This is how Python convert dictionary to list using the map() function.
7. Convert dictionary to list in Python using for loop & items() Method
In this method, we will understand the use of for loop and dict.items() method in Python convert dictionary to list.
Here, we will first use the for loop to iterate over each item in the dictionary. Then we used the list() function to convert items into a list.
Example: Here is a sample example of this execution in Python.
User = {
"Name":"James",
"Age":25,
"City": "New York",
"Country": "United States"
}
user_data = []
for key, val in User.items():
user_data.append([key, val])
print(user_data)
- In the above example, we defined a dictionary named user which consists of multiple key-value pairs.
- After this, we defined an empty dictionary named user_data. Next, we utilized the for loop and item() method to fetch each key value from the dictionary and append it to the new list.
[['Name', 'James'], ['Age', 25], ['City', 'New York'], ['Country', 'United States']]
The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.
8. How to convert dictionary into list in Python using Iteration Method
In this section, we will understand how to use iteration and the list() function in Python convert dictionary to list.
The example for the execution of the above Python program is given below.
User = {
"Name":"James",
"Age":25,
"City": "New York",
"Country": "United States"
}
user_data = []
for item in User:
k = [item, User[item]]
user_data.append(k)
print(user_data)
Output: In the above Python program, we utilized for loop to iterate over each item and used square brackets ([]) to have each key-value pair as a list. In the last, we used the list.append() method to append each key-value pair list into the list.
[['Name', 'James'], ['Age', 25], ['City', 'New York'], ['Country', 'United States']]
Here is the final result of the above example.
9. How to convert a dictionary to a list in Python using collection.namedtuple() function.
In this method, we will discuss how Python convert dictionary to list using collection.namedtuple() function.
This subclass enables us to define new tuple types, each of which has a named field with a certain type. However, these named tuples are also immutable just like standard tuples in Python.
In this approach, we will convert the dictionary key-value pair to a named tuple using collection.namedtuple() class. After this, we will store these named tuples in a list.
An example of this approach in Python convert dictionary to list is given below.
from collections import namedtuple
User = {
"Name":"James",
"Age":25,
"City": "New York",
"Country": "United States"
}
list_of_tuple = namedtuple('List', 'name value')
new_list = list(list_of_tuple(*item) for item in User.items())
print(new_list)
Output: In the example, we are converting the User dictionary to new_list list type value using collection.namedtuple() in Python.
[List(name='Name', value='James'), List(name='Age', value=25), List(name='City', value='New York'), List(name='Country', value='United States')]
Here is the final result of the above Python program.
Conclusion
So, in this Python tutorial, we understood how to Python convert dictionary to list using the following 9 methods.
- Using items() Method
- Using keys() Method
- Using values() Method
- Using loop + items() Method
- Using List Comprehension Method
- Using Zip Function
- Using Iteration Method
- Using Collection Method
- Using Map Function
You may also like reading the following articles.
- How to Delete a Dictionary in Python
- Python dictionary find a key by value
- Python dictionary pop
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.