Python - Convert List to Dictionary
Python List to Dictionary
There are many ways in which you can interpret the data in list as keys and values, and convert this list into a dictionary.
Some of the formats of lists are
[key_1, value_1, key_2, value_2, ...]
- Key:Value pairs as continuous elements of list.[key_1, key_2, ...], [value_1, value_2, ...]
- Keys are in one list, and Values are in another list.[(key_1, value_1), (key_2, value_2), ...]
Key:Value pairs as tuples, and these tuples are elements of the list.
Also, some of the other scenarios are:
- Converting Python List to dictionary with list element as key and index as value.
- Converting Python List to dictionary with list element as key and a default for value.
In this tutorial, we will learn how to convert these formats of list to dictionary, with the help of well detailed example programs.
Examples
1. Convert given list to a dictionary
In this example, we will convert the list of format [key_1, value_1, key_2, value_2, ...]
to dictionary of {key_1:value_1, key_2:value_2, ...}
.
We will use dictionary comprehension to convert this type of list to dictionary.
Python Program
myList = ['a', 'apple', 'b', 'banana', 'c', 'cherry']
myDict = {myList[i]: myList[i + 1] for i in range(0, len(myList), 2)}
print(myDict)
Output
{'a': 'apple', 'b': 'banana', 'c': 'cherry'}
2. Convert a list of keys and a list of values into a dictionary
In this example, we will convert a list of format [(key_1, value_1), (key_2, value_2), ...]
to dictionary of {key_1:value_1, key_2:value_2, ...}
.
We will use dictionary comprehension to convert this lists of keys and values to dictionary.
Python Program
listKeys = ['a', 'b', 'c']
listValues = ['apple', 'banana', 'cherry']
myDict = {listKeys[i]: listValues[i] for i in range(0, len(listKeys), 1)}
print(myDict)
Output
{'a': 'apple', 'b': 'banana', 'c': 'cherry'}
3. Convert list of tuples into a dictionary
In this example, we will convert a list of format [(key_1, value_1), (key_2, value_2), ...]
to dictionary of {key_1:value_1, key_2:value_2, ...}
.
We will use dictionary comprehension to convert list of tuples to dictionary.
Python Program
myList = [('a', 'apple'), ('b', 'banana'), ('c', 'cherry')]
myDict = {myList[i][0]: myList[i][1] for i in range(0, len(myList), 1)}
print(myDict)
Output
{'a': 'apple', 'b': 'banana', 'c': 'cherry'}
4. Convert list into dictionary with index as value
In this example, we will convert a list of format [key_1, key_2, ...]
to dictionary of {key_1:0, key_2:1, ...}
.
Python Program
myList = ['a', 'b', 'c']
myDict = {myList[i]: i for i in range(0, len(myList), 1)}
print(myDict)
Output
{'a': 0, 'b': 1, 'c': 2}
5. Convert list of keys into a dictionary with a default value
In this example, we will convert a list of format [key_1, key_2, ...]
to dictionary of {key_1:default_value, key_2:default_value, ...}
.
Python Program
myList = ['a', 'b', 'c']
defaultValue = 54
myDict = {myList[i]: defaultValue for i in range(0, len(myList), 1)}
print(myDict)
Output
{'a': 54, 'b': 54, 'c': 54}
Summary
In this tutorial of Python Examples, we learned how to convert a Python List to Dictionary.