Python
Dictionary
List
Tuples
Data Conversion

How can I convert a dictionary into a list of tuples?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, the standard way to convert a dictionary into a list of key-value tuples is to use dict.items() and wrap the result with list(). That gives you a concrete list where each element is a two-item tuple containing a key and its associated value.

The Basic Conversion

dict.items() returns a dynamic view over the dictionary's key-value pairs. Converting that view to a list produces the tuple structure most people want.

python
1scores = {"alice": 95, "bob": 88, "carol": 91}
2
3pairs = list(scores.items())
4print(pairs)

The output is:

python
[('alice', 95), ('bob', 88), ('carol', 91)]

Each tuple is (key, value).

Why items() Is the Right Tool

You could build the same result manually with a loop, but items() already exposes exactly the data you need.

python
pairs = []
for key, value in scores.items():
    pairs.append((key, value))

That works, but list(scores.items()) is shorter, clearer, and idiomatic.

If You Only Need an Iterator

Sometimes you do not need a list yet. In that case, keep the view object from items() or iterate over it directly.

python
for key, value in scores.items():
    print(key, value)

This avoids creating a new list in memory unnecessarily. Use the full list conversion only when you need indexing, serialization, repeated reuse, or an API that explicitly expects a list of tuples.

Sorting the Result

If you want the tuples in a predictable custom order, sort them after conversion or sort the items directly.

python
1scores = {"alice": 95, "bob": 88, "carol": 91}
2
3by_name = sorted(scores.items())
4by_score = sorted(scores.items(), key=lambda pair: pair[1], reverse=True)
5
6print(by_name)
7print(by_score)

This is useful when you need ranked output or stable presentation order.

Dictionary Order in Modern Python

In modern Python, dictionaries preserve insertion order. That means list(my_dict.items()) reflects the order in which entries were added to the dictionary.

python
1data = {}
2data["x"] = 1
3data["y"] = 2
4data["z"] = 3
5
6print(list(data.items()))

The resulting list keeps the x, y, z order.

That makes the conversion predictable in current Python versions, but you should still sort explicitly if the consumer depends on a specific logical order rather than insertion history.

Variations You May Actually Need

If you only want the keys as one-item tuples:

python
keys_as_tuples = [(key,) for key in scores]

If you want the values only:

python
values = list(scores.values())

If you want a transformed list of tuples, use a comprehension:

python
upper_pairs = [(key.upper(), value * 2) for key, value in scores.items()]
print(upper_pairs)

This is often more useful than the raw conversion when the tuples are being prepared for database inserts or charting libraries.

Nested Dictionaries

If the dictionary values are themselves complex objects, the tuples still work the same way:

python
1users = {
2    "alice": {"age": 30, "city": "Toronto"},
3    "bob": {"age": 28, "city": "Montreal"},
4}
5
6pairs = list(users.items())
7print(pairs)

The second element in each tuple is simply the original value object, even if that value is another dictionary or a list.

Common Pitfalls

The first pitfall is using list(my_dict) and expecting key-value tuples. That expression returns only the keys, because iterating a dictionary yields keys by default.

Another pitfall is assuming items() already returns a list. In Python 3, it returns a view object, which is iterable but not itself a list.

A third pitfall is forgetting the tuple shape. Each element is a two-item tuple, so access patterns should look like pair[0] for the key and pair[1] for the value, or better yet, unpack the tuple directly.

Finally, convert to a list only when you need one. For simple iteration, the items() view is already efficient and readable.

Summary

  • The idiomatic conversion is list(my_dict.items())
  • Each result element is a (key, value) tuple
  • Use items() directly when you only need iteration and not a concrete list
  • Sort the tuples if you need a specific order beyond insertion order
  • Avoid list(my_dict) when you actually want key-value pairs

Course illustration
Course illustration

All Rights Reserved.