In this Python tutorial, we will understand how Python convert dictionary to list using examples.
In Python, we can convert dictionary to list in Python 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
Python convert dictionary to list
Here we will cover all 9 methods to convert dictionary to list in Python with various examples.
Method 1: By using items() method
In this section, we will understand how to use the items() method to convert a dictionary to a list in Python.
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:
Read: How to Create a Dictionary from one list in Python
Method 2: By 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)
Here is the Screenshot of the following given code
Read: Python Dictionary update
Method 3: By 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.
Here is the sample code in Python.
to_dict ={"i":160,"w":110,"s":340}
m = list(zip(to_dict.keys(), to_dict.values()))
print("Convert dictionary to list:",m)
Here is the implementation of the following given code
Read: Python dictionary of lists
Method 4: By 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:
Read: Python dictionary list of tuples
Method 5: By 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.
n_dictionary ={"James":240,"William":180,"Chris":140}
n_lis = [(key, value) for key, value in n_dictionary.items()]
print(n_lis)
Check the execution of the following given code
Read: Python dictionary multiple keys
Method 6: By 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:
This is how to convert a dictionary to a list.
Read: Python dictionary find a key by value
Method 7: Using for loop & items() Method
In this method, we will understand the use of for loop and dict.items() method to convert a dictionary to a list in Python
Here, we will first use the for loop to iterate over each item in the dictionary. And then we used the list() function to convert items into a list.
Here is a sample example of this execution in Python.
# Defining a dictionary
User = {
"Name":"James",
"Age":25,
"City": "New York",
"Country": "United States"
}
# Defining a list
user_data = []
# Using for loop & items()
for key, val in User.items():
user_data.append([key, val])
# Printing list
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.
Here is the final execution of the above Python program.
Read: Get First Key in dictionary Python
Method 8: Using Iteration Method
In this section, we will understand how to use iteration and the list() function in Python to convert a dictionary to a list.
The example for the execution of the above Python program is given below.
# Defining a dictionary
User = {
"Name":"James",
"Age":25,
"City": "New York",
"Country": "United States"
}
# Defining a list
user_data = []
# Using iteration
for item in User:
k = [item, User[item]]
user_data.append(k)
# Printing list
print(user_data)
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.
Here is the final result of the above example.
Read: Get all values from a dictionary Python
Method 9: Using collection.namedtuple()
In this method, we will discuss how to convert dictionary to list using collection.namedtuple().
The collection.namedtuple() is a subclass in the Python collection module.
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 dictionary key-value pair to named tuple using collection.namedtuple() class. After this, we will store these named tuples into a list.
The example for this approach in Python is given below.
from collections import namedtuple
# Defining a dictionary
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())
# Printing list
print(new_list)
In the example, we are converting the User dictionary to new_list list type value using collection.namedtuple() in Python.
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 convert Python degrees to radians
- Python convert tuple to list
- How to convert list to string in Python
- Python dictionary find a key by value
- Python List index() method [With Examples]
- 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.