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.

Example 1: Convert List to 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) 
Run

Output

{'a': 'apple', 'b': 'banana', 'c': 'cherry'}

Example 2: Convert Lists of Keys and Values to 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) 
Run

Output

{'a': 'apple', 'b': 'banana', 'c': 'cherry'}

Example 3: Convert List of Tuples to 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) 
Run

Output

{'a': 'apple', 'b': 'banana', 'c': 'cherry'}

Example 4: Convert List of 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) 
Run

Output

{'a': 0, 'b': 1, 'c': 2}

Example 5: Convert List of Dictionary with 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) 
Run

Output

{'a': 54, 'b': 54, 'c': 54}

Summary

In this tutorial of Python Examples, we learned how to convert a Python List to Dictionary.