Convert Dictionary to List of Tuples - Python
Last Updated :
11 Jul, 2025
Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2), ('c', 3)]. Let's explore different ways to achieve this conversion.
Using items()
items() method returns an iterable view of a dictionary's key-value pairs as tuples. It is useful for iterating through the dictionary or converting it to a list of tuples.
Python
d = {'a': 1, 'b': 2, 'c': 3}
res = list(d.items())
print(res)
Output[('a', 1), ('b', 2), ('c', 3)]
Explanation:
- items() method is used to retrieve the dictionary's key-value pairs as tuples.
- list() function converts the iterable returned by items() into a list of tuples, resulting in a list like [('a', 1), ('b', 2), ('c', 3)].
Using List Comprehension
List comprehension allows creating a list by iterating over an iterable and applying an expression. For dictionaries, it can be used to extract key-value pairs or transform them into tuples, lists or other formats.
Python
d = {'a': 1, 'b': 2, 'c': 3}
res = [(key, val) for key, val in d.items()]
print(res)
Output[('a', 1), ('b', 2), ('c', 3)]
Explanation:
- List comprehension iterates over the dictionary d's key-value pairs using d.items(), creating tuples of each key and value.
- Resulting list consists of tuples like [('a', 1), ('b', 2), ('c', 3)], where each tuple represents a key-value pair from the dictionary.
Using map() Function
map() function in Python applies a given function to each item of an iterable (like a list or a dictionary's items) and returns an iterator of the results. It's often used for transforming or processing elements in an iterable efficiently.
Python
d = {'a': 1, 'b': 2, 'c': 3}
# map key-value pairs to tuples
res = list(map(lambda item: item, d.items()))
print(res)
Output[('a', 1), ('b', 2), ('c', 3)]
Explanation:
- map() function applies the lambda function to each key-value pair in the dictionary d, extracting the key and value as a tuple.
- list() function converts the result of map() into a list of tuples, producing a result like [('a', 1), ('b', 2), ('c', 3)].
Using for Loop
Using a for loop to convert a dictionary to a list of tuples involves iterating over each key-value pair and appending them as tuples to a new list. This provides a straightforward way to collect dictionary items into tuples.
Python
d = {'a': 1, 'b': 2, 'c': 3}
# Store key-value pairs as tuples
res = []
# Append each key-value pair to the list
for key, val in d.items():
res.append((key, val))
print(res)
Output[('a', 1), ('b', 2), ('c', 3)]
Explanation:
- for loop iterates over each key-value pair in the dictionary d using the items() method.
- In each iteration, the key and value are appended as a tuple to the list res, resulting in a list of tuples like [('a', 1), ('b', 2), ('c', 3)].
Related Articles:
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice