Skip to content

Python dictionary to list

  • by

Converting a Python dictionary to a list can be done in multiple ways depending on how you want the list to be structured. Here are a few common methods:

List of keys: If you only want to extract the keys from the dictionary and store them in a list, you can use the keys() method of the dictionary.

my_dict = {'a': 1, 'b': 2, 'c': 3}
key_list = list(my_dict.keys())

List of values: To get a list containing all the values from the dictionary, you can use the values() method.

my_dict = {'a': 1, 'b': 2, 'c': 3}
value_list = list(my_dict.values())

List of key-value pairs (tuples): If you want to create a list of tuples where each tuple contains a key-value pair, you can use the items() method.

my_dict = {'a': 1, 'b': 2, 'c': 3}
item_list = list(my_dict.items())

Flattened list of keys and values: If you want a single list containing both keys and values, you can combine the keys and values lists using the + operator or using list comprehension.

Using the + operator:

my_dict = {'a': 1, 'b': 2, 'c': 3}
key_list = list(my_dict.keys())
value_list = list(my_dict.values())
flat_list = key_list + value_list


Using list comprehension:

my_dict = {'a': 1, 'b': 2, 'c': 3}
flat_list = [item for pair in my_dict.items() for item in pair]

Choose the method that suits your specific use case based on how you want the dictionary elements to be represented in the list.

How to convert Python Dictionary to a list example

We can combine all the methods into a single code snippet like this:

my_dict = {'a': 1, 'b': 2, 'c': 3}

#  list & items() method
item_list = list(my_dict.items())

#  keys() method
key_list = list(my_dict.keys())

#  values() method
value_list = list(my_dict.values())

#  list comprehension
item_list_comp = [(key, value) for key, value in my_dict.items()]

#  zip() function
key_list_zip, value_list_zip = zip(*my_dict.items())

#  map() function
key_list_map = list(map(lambda x: x[0], my_dict.items()))
value_list_map = list(map(lambda x: x[1], my_dict.items()))

#  for loop & items() method
item_list_loop = []
for key, value in my_dict.items():
    item_list_loop.append((key, value))

print(" list & items() method:", item_list)
print(" keys() method:", key_list)
print(" values() method:", value_list)
print(" list comprehension:", item_list_comp)
print(" zip() function for keys:", list(key_list_zip))
print(" zip() function for values:", list(value_list_zip))
print(" map() function for keys:", key_list_map)
print(" map() function for values:", value_list_map)
print(" for loop & items() method:", item_list_loop)


Python dictionary to list

This code will print the results of all the different methods to convert the dictionary to lists. Keep in mind that each list in the output will be identical since they all represent the same data from the original dictionary. The different methods are provided for demonstration purposes to show various ways of achieving the same result.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading