How to Convert a List to a Dictionary in Python
Converting a list into a dictionary is a common data transformation task in Python. A list is an ordered sequence of items, while a dictionary is an unordered collection of key-value pairs. Because their structures are fundamentally different, the method you use for conversion depends entirely on the format of your input list(s).
This guide will cover the most common scenarios you'll encounter when converting a list to a dictionary, providing the best Pythonic solution for each case, including using dict.fromkeys(), the zip() function, and dictionary comprehensions.
Scenario 1: Creating a Dictionary from a List of Keys
If you have a single list that you want to use as the keys of a new dictionary, with each key having the same default value, the dict.fromkeys() class method is the perfect tool.
Solution: this method takes an iterable (your list) for the keys and an optional default value. If no value is provided, it defaults to None.
keys = ["name", "age", "nationality"]
# Solution 1: Using a default value of None
dict_from_keys_none = dict.fromkeys(keys)
print(f"Dictionary with None values: {dict_from_keys_none}")
# Solution 2: Providing a specific default value
dict_from_keys_default = dict.fromkeys(keys, "unknown")
print(f"Dictionary with a default value: {dict_from_keys_default}")
Output:
Dictionary with None values: {'name': None, 'age': None, 'nationality': None}
Dictionary with a default value: {'name': 'unknown', 'age': 'unknown', 'nationality': 'unknown'}
Scenario 2: Zipping Two Lists (Keys and Values) into a Dictionary
This is a very common scenario where you have two separate lists: one containing the keys and another containing the corresponding values. The zip() function is ideal for this, as it pairs elements from each list together.
Solution:
- The
zip()function creates an iterator of tuples, where each tuple contains one element from each of the input lists. - Thedict()constructor can then directly convert this iterator of pairs into a dictionary.
keys = ["name", "age", "nationality"]
values = ["Jane", 29, "German"]
# Pair the keys and values with zip() and convert to a dictionary with dict()
person_dict = dict(zip(keys, values))
print(f"Resulting dictionary: {person_dict}")
print(f"Accessing a value: {person_dict['name']}")
Output:
Resulting dictionary: {'name': 'Jane', 'age': 29, 'nationality': 'German'}
Accessing a value: Jane
If the lists have different lengths, zip() will stop as soon as the shortest list is exhausted.
Scenario 3: Converting an Alternating Key-Value List
Sometimes, you might have a single "flat" list where keys and values alternate (e.g., [key1, value1, key2, value2, ...]). A dictionary comprehension is the most elegant solution for this structure.
Solution: we can iterate through the list with a step of 2 to get the index of each key. The value will be at the next index (index + 1).
person_list = ["name", "Jane", "age", 29, "nationality", "German"]
# Use a dictionary comprehension with a stepped range
person_dict = {
person_list[i]: person_list[i + 1] for i in range(0, len(person_list), 2)
}
print(f"Resulting dictionary: {person_dict}")
Output:
Resulting dictionary: {'name': 'Jane', 'age': 29, 'nationality': 'German'}
This comprehension iterates through the indices 0, 2, 4, setting person_list[0] as a key for person_list[1], person_list[2] as a key for person_list[3], and so on.
Scenario 4: Converting a List of Key-Value Pairs
If your list is already structured as a list of pairs (e.g., a list of tuples or a list of lists), the dict() constructor can convert it directly. This is the simplest conversion scenario.
Solution: the dict() constructor is specifically designed to handle an iterable of key-value pairs.
# A list of tuples
list_of_tuples = [('name', 'Jane'), ('age', 29), ('nationality', 'German')]
dict_from_tuples = dict(list_of_tuples)
print(f"From list of tuples: {dict_from_tuples}")
# A list of lists
list_of_lists = [['name', 'Jane'], ['age', 29], ['nationality', 'German']]
dict_from_lists = dict(list_of_lists)
print(f"From list of lists: {dict_from_lists}")
Output:
From list of tuples: {'name': 'Jane', 'age': 29, 'nationality': 'German'}
From list of lists: {'name': 'Jane', 'age': 29, 'nationality': 'German'}
Conclusion
The correct way to convert a list to a dictionary in Python depends on the structure of your starting data.
| If your input is... | The best solution is... |
|---|---|
| A single list of keys | dict.fromkeys(my_list, default_value) |
| Two separate lists for keys and values | dict(zip(keys_list, values_list)) |
| A single list of alternating keys and values | A dictionary comprehension with a step |
| A list of key-value pairs (tuples or lists) | The dict() constructor: dict(my_list) |
By identifying your starting scenario, you can choose the most efficient and readable method for the conversion.