Python - Convert Dictionary to List
Python - Convert Dictionary to List
To convert Dictionary to List in Python, you can call list() built-in function and pass the dictionary items from items() method as argument to list(). The function returns a list of tuples created from the given dictionary. To customize the value during conversion, you may use List comprehension instead of list() function.
In this tutorial, you will learn how to convert a given dictionary to a list, with examples.
For example, consider the following dictionary.
{
'foo':12,
'bar':14
}
'foo'
and 'bar'
are keys. 12 and 14 are values. If we would like to convert this dictionary to a list, then the resulting list should look like the following.
[('foo', 12), ('bar', 14)]
Each item in the dictionary would become a tuple inside the list. So, effectively, a dictionary is converted to a list of tuples.
1. Converting dictionary to list using list() in Python
Consider that you are given a dictionary my_dict with some initial key-value pairs. You have to convert this dictionary to a list using Python list() built-in function.
Steps
- Given a dictionary in my_dict.
- Call list() built-in function, and pass the dictionary items my_dict.items() as argument to the function. The list() function returns a list created from the items in the dictionary.
- Store the returned list in a variable, say converted_list. You may print this list to output using print() built-in function.
Dictionary items() method returns dict_items iterator.
Program
The program to convert given dictionary to a list using list() built-in function is given below.
Python Program
my_dict = {
'foo':12,
'bar':14
}
converted_list = list(my_dict.items())
print(converted_list)
Output
[('foo', 12), ('bar', 14)]
2. Converting dictionary to list using List Compression in Python
In the following program, you are given a dictionary my_dict with some initial key-value pairs. You have to convert this dictionary to a list using Python List Comprehension.
Steps
- Given a dictionary in my_dict.
- Use list comprehension, and for each item in the dictionary items my_dict.items(), add a tuple to the list.
- Store the returned list in a variable, say converted_list. You may print this list to output using print() built-in function.
Using list comprehension gives us flexibility to modify the values when we are converting the items from dictionary to list.
Program
Python Program
my_dict = {
'foo':12,
'bar':14
}
converted_list = [(key, value) for key, value in my_dict.items()]
print(converted_list)
Output
[('foo', 12), ('bar', 14)]
Now, let us see how list comprehension gives us flexibility to change the values during version.
Say, that we want the value to be doubled while converting to the list. We can specify 2*value in the expression of List Comprehension, as shown in the following program.
Python Program
my_dict = {
'foo':12,
'bar':14
}
converted_list = [(key, 2*value) for key, value in my_dict.items()]
print(converted_list)
Output
[('foo', 24), ('bar', 28)]
Summary
In this tutorial, we learned how to convert a given dictionary to a list using list() built-in function, or List Comprehension, with the help of well detailed Python programs.