5 Best Ways to Convert Python Dict to List

πŸ’‘ Problem Formulation: Converting a Python dictionary to a list is a common task that can be approached in several ways. Whether you want to convert just the keys, values, or both as a list of tuples, understanding the most efficient methods is essential. For example, given the dictionary {'a': 1, 'b': 2, 'c': 3}, how would we convert this to a list of keys, ['a', 'b', 'c'], a list of values, [1, 2, 3], or a list of key-value pairs, [('a', 1), ('b', 2), ('c', 3)]?

Method 1: Using list() on dict.keys()

Convert the keys of a dictionary into a list using the built-in dict.keys() method and list(). This method returns a view of keys that can be explicitly converted into a list.

Here’s an example:

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

Output: ['a', 'b', 'c']

This code obtains the keys from the dictionary using the keys() method and then converts the keys view into a list by passing it to the list() constructor.

Method 2: Using list() on dict.values()

To extract values from a dictionary and create a list, use the dict.values() method with the list() constructor. This will provide a list of all values in the dictionary.

Here’s an example:

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

Output: [1, 2, 3]

This snippet converts the dictionary values, received from the values() method, into a list, preserving the order of insertion in the dictionary.

Method 3: List Comprehension for keys or values

List comprehension offers a concise way to create lists from the keys or values of a dictionary. It’s a single-line, readable method.

Here’s an example:

my_dict = {'a': 1, 'b': 2, 'c': 3}
keys_list = [key for key in my_dict]
values_list = [value for value in my_dict.values()]

Output: ['a', 'b', 'c'], [1, 2, 3]

The first list comprehension iterates over dictionary keys while the second one iterates over dictionary values, creating two lists.

Method 4: Using dict.items()

The dict.items() method combined with the list() function allows us to convert dictionary key-value pairs into a list of tuples. This preserves both keys and values.

Here’s an example:

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

Output: [('a', 1), ('b', 2), ('c', 3)]

The items() method returns a view of key-value pairs, which is then converted into a list of tuples.

Bonus One-Liner Method 5: Using a Lambda Function

The flexibility of Python allows for even more concise solutions, such as using a lambda function with the map() method to convert dictionary items to a list.

Here’s an example:

my_dict = {'a': 1, 'b': 2, 'c': 3}
items_list = list(map(lambda item: item, my_dict.items()))

Output: [('a', 1), ('b', 2), ('c', 3)]

This one-liner maps each key-value pair in the dictionary to a tuple and then creates a list with all tuples.

Summary/Discussion

  • Method 1: Using list() on dict.keys(). This method is straightforward and works best for extracting keys. However, it cannot be used to extract values or items directly.
  • Method 2: Using list() on dict.values(). Similar to Method 1 but for values. It’s simple but limited to values only.
  • Method 3: List Comprehension for keys or values. This is an expressive and Pythonic way, ideal for writing compact code. Its limitation is that it might be less readable to new Python users.
  • Method 4: Using dict.items(). This method is perfect for getting both keys and values at once. But it may not be the best choice if you need only keys or values.
  • Method 5: Using a Lambda Function. This shows the power of Python’s functional programming features but can be overkill for such a simple task, and it may impact readability.