0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert List of Tuples to Dictionary

You can convert a list of tuples to a dictionary in Python using the dict() function like this: dict(list_of_tuples).
📋

Examples

Input[('a', 1), ('b', 2)]
Output{'a': 1, 'b': 2}
Input[('name', 'Alice'), ('age', 30), ('city', 'NY')]
Output{'name': 'Alice', 'age': 30, 'city': 'NY'}
Input[]
Output{}
🧠

How to Think About It

To convert a list of tuples to a dictionary, think of each tuple as a pair where the first item is the key and the second is the value. Using the dict() function, Python automatically pairs these up to create the dictionary.
📐

Algorithm

1
Get the list of tuples as input.
2
Use the built-in <code>dict()</code> function to convert the list into a dictionary.
3
Return or print the resulting dictionary.
💻

Code

python
list_of_tuples = [('a', 1), ('b', 2), ('c', 3)]
dictionary = dict(list_of_tuples)
print(dictionary)
Output
{'a': 1, 'b': 2, 'c': 3}
🔍

Dry Run

Let's trace converting [('a', 1), ('b', 2)] to a dictionary.

1

Start with list

list_of_tuples = [('a', 1), ('b', 2)]

2

Convert using dict()

dictionary = dict(list_of_tuples)

3

Result

dictionary = {'a': 1, 'b': 2}

TupleKeyValueDictionary after step
('a', 1)a1{'a': 1}
('b', 2)b2{'a': 1, 'b': 2}
💡

Why This Works

Step 1: Using dict() function

The dict() function takes an iterable of pairs and creates a dictionary by using the first element as the key and the second as the value.

Step 2: Pairs from tuples

Each tuple in the list represents one key-value pair, so the function reads them one by one.

Step 3: Resulting dictionary

The output is a dictionary where keys are unique and values are assigned from the tuples.

🔄

Alternative Approaches

Using a for loop
python
list_of_tuples = [('a', 1), ('b', 2)]
dictionary = {}
for key, value in list_of_tuples:
    dictionary[key] = value
print(dictionary)
This method is more manual but useful if you want to add extra processing during conversion.
Using dictionary comprehension
python
list_of_tuples = [('a', 1), ('b', 2)]
dictionary = {k: v for k, v in list_of_tuples}
print(dictionary)
This is concise and allows easy filtering or transformation during conversion.

Complexity: O(n) time, O(n) space

Time Complexity

The conversion loops through each tuple once, so it takes linear time proportional to the number of tuples.

Space Complexity

A new dictionary is created with the same number of key-value pairs, so space grows linearly with input size.

Which Approach is Fastest?

Using dict() is the fastest and most readable. Manual loops or comprehensions add flexibility but are slightly slower.

ApproachTimeSpaceBest For
dict() functionO(n)O(n)Simple and fast conversion
For loopO(n)O(n)When extra processing needed
Dictionary comprehensionO(n)O(n)Concise with filtering or transformation
💡
Use dict() directly on the list of tuples for the simplest conversion.
⚠️
Trying to convert a list of tuples with more or less than two elements per tuple will cause an error.