How to convert a list to a dictionary in Python?

Convert a List To a Dictionary In Python

In this tutorial, we will discuss how we can convert a list to a dictionary in Python.

Also read: How to convert string to list in Python?

What is a list in Python?

A list in Python is a linear data structure used to store a collection of values that may be of the same type or different types. Each item inside a list is separated by a comma (,).

A Python list is enclosed in the square brackets []. A Python list is mutable, meaning we can change or modify its contents after its creation inside the Python program.

A Python list stores the elements in an ordered manner and we can access any element in the list directly by its index. This is how we can create a list in Python and we can also validate it if it’s a list or not using the type() function:

# Defining a Python list

ls = ['AskPython', 'JournalDev', 'LinuxforDevices']

# Printing the results
print(ls)

# Validating the type of 'ls'
print(type(ls))

Output:

['AskPython', 'JournalDev', 'LinuxforDevices']
<class 'list'>

What is a dictionary in Python?

A dictionary in Python is a special type of data structure that is used to store the data in the key-value pair format. A Python dictionary has two main components: key & value where the key must be a single entity but value can be a multi-value entity like list, tuple, etc.

The key and its value are separated by a colon (:) and each item (a key-value pair) is separated by a comma (,) just like the Python list. A dictionary is enclosed in the curly brackets {}. Like the Python list, the Python dictionary is also mutable.

A Python dictionary stores the elements in an unordered manner and we can access any value in the dictionary directly by its key. This is how we can create a dictionary in Python and just like the list we can also validate if it’s a dictionary or not using the type() function:

# Defining a Python dictionary
ds = {'AskPython': "Python", 'JournalDev': "Java, Android, Python, Web", 'LinuxforDevices': "Unix/Linux"}

# Printing the results
print(ds)

# Validating the type of 'ds'
print(type(ds))

Output:

{'AskPython': 'Python', 'JournalDev': 'Java, Android, Python, Web', 'LinuxforDevices': 'Unix/Linux'}
<class 'dict'>

Mainly there are two methods for converting a list to a dictionary in Python one is dictionary comprehension and the other one is using the Python zip() method. We are going to discuss them one by one.

Also read: Python list of dictionaries

Convert a list to a dictionary using dictionary comprehension

In this method of converting a list to a dictionary, we iterate through the given Python list and make the corresponding key:value inside the curly brackets {}. Each list element becomes a key of the dictionary and we can generate a value for each key depending upon our usage. Let’s see one example which uses dictionary comprehension to convert a Python list to a Python dictionary. Following is the Python code to demonstrate it:

# Defining a Python list
ls = ['E', 'J', 'O', 'T', 'Y']

# Using dictionary comprehension to convert the list to a dictionary
ds = {item: ord(item) for item in ls}

# Printing the results
print("Given Python list: ", ls)
print("Generated Python dictionary: ", ds)

# Validating the type of 'ds'
print(type(ds))

Output:

Given Python list:  ['E', 'J', 'O', 'T', 'Y']
Generated Python dictionary:  {'E': 69, 'J': 74, 'O': 79, 'T': 84, 'Y': 89}
<class 'dict'>

In the above example, we have used the Python ord() function to get the ASCII value of each item inside the list which is actually an English alphabet. And assigned the ASCII value returned by the ord() function to the corresponding key of the dictionary as its value. Hence we have generated a key-value pair for each item inside the list and converted it to a Python dictionary.

Convert a list to a dictionary using the Python zip() function

In this method, we create an iterator using the Python iter() function which will iterate through the given Python list. And then we will use the Python zip() function which actually zips two items together and makes the corresponding key:value.

And as the last step, we do the typecasting using the Python dict() function which creates and returns a Python dictionary from the zipped values returned by the zip() function.

Let’s see one example which uses the Python zip() function to convert a Python list to a Python dictionary. Following is the Python code to demonstrate it:

# Defining a Python list
ls = ['E', 69, 'J', 74, 'O', 79, 'T', 84, 'Y', 89]

# Creating an iterator for list 'ls'
item = iter(ls)

# Using the Python zip() function to convert the list to a dictionary
ds = dict(zip(item, item))

# Printing the results
print("Given Python list: ", ls)
print("Generated Python dictionary: ", ds)

# Validating the type of 'ds'
print(type(ds))

Output:

Given Python list:  ['E', 69, 'J', 74, 'O', 79, 'T', 84, 'Y', 89]
Generated Python dictionary:  {'E': 69, 'J': 74, 'O': 79, 'T': 84, 'Y': 89}
<class 'dict'>

Conclusion

In this Python tutorial, we have learned very useful and handy techniques to convert a Python list to a Python dictionary. We have also learned about the various useful Python functions like iter(), zip(), dict(), and ord(). Hope you are excited to do more fun learning with these Python functions.