This post will discuss how to convert a dictionary into a list of (key, value) pairs in Python.

For example, the dictionary {'A': 1, 'B': 2, 'C': 3} should be converted to [('A', 1), ('B', 2), ('C', 3)].

1. Using dict.items() function

The standard solution is to use the built-in function dict.items() to get a view of objects of (key, value) pairs present in the dictionary.

Download  Run Code

 
Since the objects returned by dict.items() are just a dynamic view on the dictionary’s entries, you can use the list constructor to get a list, as shown below:

Download  Run Code

2. Using dict.keys() function

Alternatively, you can use the dict.keys() function to construct the list of dictionary’s (key, value) pairs. This can be easily achieved with List Comprehension, as shown below:

Download  Run Code

 
Another way to create the same list is pairs = [(k, v) for (k, v) in d.items()].

Download  Run Code

3. Using zip() function

In the Python dictionary, keys and values are iterated over in insertion order. This allows creating (key, value) pairs using zip(), as shown below:

Download  Run Code

That’s all about converting a dictionary into a list of pairs in Python.