How to Convert List to Dictionary in Python
There are several ways to convert a list into a dictionary in Python, depending on the structure of the list and the desired key-value mapping. Here are some common methods:
1. Using zip()
Function
If you have two lists, one containing keys and the other containing values, you can use the zip()
function to pair them and convert them into a dictionary.
Example:
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary)
Output:
{'a': 1, 'b': 2, 'c': 3}
- How it works: The
zip()
function combines elements from the two lists into tuples, anddict()
converts these tuples into key-value pairs.
2. Using Dictionary Comprehension
You can use a dictionary comprehension to create a dictionary directly from a list by specifying the key-value mapping logic.
Example:
list_data = [1, 2, 3, 4]
dictionary = {x: x**2 for x in list_data}
print(dictionary)
Output:
{1: 1, 2: 4, 3: 9, 4: 16}
- How it works: The comprehension iterates over the list, assigning each item as a key and performing a transformation (e.g., squaring) to generate the value.
3. Converting a List of Tuples
If the list already contains key-value pairs as tuples, you can directly convert it into a dictionary using the dict()
constructor.
Example:
list_of_tuples = [('a', 1), ('b', 2), ('c', 3)]
dictionary = dict(list_of_tuples)
print(dictionary)
Output:
{'a': 1, 'b': 2, 'c': 3}
- How it works: The
dict()
constructor takes an iterable of pairs and creates a dictionary.
4. Using enumerate()
for a List of Values
If you only have a single list and want to use the indices as keys, you can use the enumerate()
function.
Example:
values = [10, 20, 30]
dictionary = dict(enumerate(values))
print(dictionary)
Output:
{0: 10, 1: 20, 2: 30}
- How it works:
enumerate()
generates pairs of index and value, whichdict()
uses to create the dictionary.
5. Handling Nested Lists
If the list contains nested lists where each sublist represents a key-value pair, you can use the dict()
constructor.
Example:
nested_list = [['a', 1], ['b', 2], ['c', 3]]
dictionary = dict(nested_list)
print(dictionary)
Output:
{'a': 1, 'b': 2, 'c': 3}
- How it works: The
dict()
constructor treats each sublist as a pair of key and value.
6. Using fromkeys()
Method
If you want all keys to have the same value, you can use the dict.fromkeys()
method.
Example:
keys = ['a', 'b', 'c']
value = 0
dictionary = dict.fromkeys(keys, value)
print(dictionary)
Output:
{'a': 0, 'b': 0, 'c': 0}
- How it works:
fromkeys()
creates a dictionary with keys from the list and assigns the same value to all keys.
Key Considerations
- Unique Keys: A dictionary requires unique keys; if the key is duplicated from the list only the last element will be present.
- Data Structure: The list formed should match the desired dictionary format, such as tuples, sublists, or two lists.
- Error Handling: Be aware of list lengths when using
zip()
; if lists are of unequal length, extra elements in the longer list are ignored.