|
| Home |
|---|
To convert a Python list to a dictionary, use the dict() function on a list of key-value tuples, combine two lists with zip(), or build one manually with a loop or dictionary comprehension — the right method depends on how your list data is structured.
In this tutorial you will learn every common way to convert a list to a dictionary, how to assign index numbers as keys, how to set a default value for every key, how to handle lists of unequal length, real use cases, and the mistakes beginners make most often.
If your list already contains key-value pairs as tuples, pass it directly into dict(). Each tuple must contain exactly two elements — the first becomes the key, the second becomes the value.
dict(list_of_tuples)
data = [
("name","Alice"),
("age",21),
("course","Python")
]
result = dict(data)
print(result)
{'name': 'Alice', 'age': 21, 'course': 'Python'}
dict() reads each tuple in order and builds one key-value entry per tuple. This is the most direct conversion when your data is already paired.
When you have two separate lists — one of keys, one of values — use zip() to pair them up, then wrap the result in dict().
dict(zip(keys_list, values_list))
keys = ["name","age","course"]
values = ["Alice",21,"Python"]
result = dict(zip(keys, values))
print(result)
{'name': 'Alice', 'age': 21, 'course': 'Python'}
zip() pairs each element from keys with the element at the same position in values, producing tuples that dict() converts into key-value pairs. This is the most common and most readable way to build a dictionary from two parallel lists.
A loop gives you full control when you need extra logic during the conversion, such as transforming values or skipping certain entries.
keys = ["name","age","course"]
values = ["Alice",21,"Python"]
student = {}
for i in range(len(keys)):
student[keys[i]] = values[i]
print(student)
{'name': 'Alice', 'age': 21, 'course': 'Python'}
The loop walks through both lists using a shared index and assigns each key its matching value. This approach is slower to write than zip() but is useful when each assignment needs extra processing.
A dictionary comprehension combines the readability of zip() with the flexibility of a loop, letting you transform or filter values in a single line.
keys = ["name","age","course"]
values = ["alice",21,"python"]
student = {k: v.upper() if isinstance(v, str) else v for k, v in zip(keys, values)}
print(student)
{'name': 'ALICE', 'age': 21, 'course': 'PYTHON'}
This comprehension pairs the two lists with zip(), then applies a transformation — uppercasing string values — as each entry is added to the dictionary.
If you only have one list and want to use each item's position as its key, use enumerate().
fruits = ["apple","banana","cherry"]
result = dict(enumerate(fruits))
print(result)
{0: 'apple', 1: 'banana', 2: 'cherry'}
enumerate() generates (index, value) pairs from the list, which dict() then turns into a dictionary keyed by position. This is useful for quickly indexing a list without writing a manual loop.
To build a dictionary where every key from a list starts with the same default value, use dict.fromkeys().
keys = ["math","science","english"]
result = dict.fromkeys(keys, 0)
print(result)
{'math': 0, 'science': 0, 'english': 0}
This is a common pattern for initializing counters or trackers — for example, setting every subject's score to 0 before updating them later in the program.
When using zip() with two lists of different lengths, Python silently stops at the shorter list instead of raising an error.
keys = ["name","age","course","grade"]
values = ["Alice",21,"Python"]
result = dict(zip(keys, values))
print(result)
{'name': 'Alice', 'age': 21, 'course': 'Python'}
The fourth key, "grade", is silently dropped because there's no matching value. If you need to catch this instead of ignoring it, check len(keys) == len(values) before calling zip(), or use itertools.zip_longest() to fill missing values with a default.
zip()dict.fromkeys() to initialize counters before tallying occurrencesdict(list_of_tuples)dict(zip(keys, values))dict(enumerate(list))dict.fromkeys(keys, default_value)zip() and not realizing extra items are silently droppeddict() requires pairs of key-value elements, not a flat list of valuesTypeErrordict.fromkeys() creates independent copies of a mutable default value, when all keys actually share the same object reference
Use dict() on a list of key-value tuples, or use dict(zip(keys, values)) if you have two separate lists of keys and values.
Use dict(zip(keys_list, values_list)). The zip() function pairs items from both lists by position before dict() builds the dictionary.
zip() stops as soon as the shorter list runs out, silently ignoring any extra items in the longer list. Check the lengths beforehand if this could cause data loss.
Use dict(enumerate(list)), which pairs each item with its index and builds a dictionary keyed by position.
Yes. Use dict.fromkeys(keys_list, default_value) to build a dictionary where every key starts with the same value.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net