Convert list to dictionary in Python: Different Methods

The list is a data structure that allows you to store multiple variables in a single variable. If you want to convert a list to a dictionary in python then this post is for you. Here you will know all the methods to convert it into a dictionary using various methods.

Methods to convert list to the dictionary in Python

Let’s know all the methods to convert a list to a dictionary in python.

Below is the sample list that you will use in all the methods. It will be a list of lists with each element of the inside list that will act as key-value pair.

sample_list = [["a", 10], ["b", 20], ["c", 30]]

Method 1: Using a for loop with the dict() constructor

This approach first creates an empty dictionary, and then loops over the list, adding each key-value entry to the dictionary. Run the below lines of code to convert the Convert list to the dictionary in Python.

sample_list = [["a", 10], ["b", 20], ["c", 30]]
sample_dict = {}
for key, value in sample_list:
    sample_dict[key] = value
print(sample_dict)

Output

{'a': 10, 'b': 20, 'c': 30}

Method 2: Convert the list to the dictionary in Python using dictionary comprehension

You can also use dictionary comprehension for the conversion. This method creates a new dictionary from the key-value pairs of the list. Also, It is a shorter way of doing it than with a loop you use to iterate the list.

Below is the full code for conversion.

sample_list = [["a", 10], ["b", 20], ["c", 30]]
sample_dict = {key: value for key, value in sample_list}
print(sample_dict)

Output

{'a': 10, 'b': 20, 'c': 30}

Method 3: zip() function with the dict() function

In this method, there will be two lists. One list contains keys and the other list contains values. This approach uses the zip() function to merge two distinct lists (keys and values) into a single list of key-value pairs. Lastly builds a new dictionary from that list.

keys = ["a", "b", "c"]
values = [10, 20, 30]
sample_dict = dict(zip(keys, values))
print(sample_dict)

Output

{'a': 10, 'b': 20, 'c': 30}

Method 4: Using the fromkeys()

The fromkeys() function is a part of the dict() method. It accepts two parameter keys and a values list. But here it will assign full values list as a value to each element of the keys list.

You will know more when you will run the below lines of code.

keys = ["a", "b", "c"]
values = [10, 20, 30]
sample_dict = dict.fromkeys(keys,values)
print(sample_dict)

Output

{'a': [10, 20, 30], 'b': [10, 20, 30], 'c': [10, 20, 30]}

Method 5: Using pandas with to_dict() function

You will first use the pandas library, to convert the list into a dataframe. After that, the dataframe is converted into a dictionary using the to_dict() method.

import pandas as pd
sample_list = [["a", 10], ["b", 20], ["c", 30]]
sample_df = pd.DataFrame(sample_list, columns = ['key', 'value'])
sample_dict = sample_df.set_index('key').T.to_dict('list')
print(sample_dict)

Output

{'a': [10], 'b': [20], 'c': [30]}

Conclusion

In this article, you have explored various methods to transform a list into a dictionary in Python. Here I have considered the use of zip(), dict comprehensions, and the fromKeys() method, etc. Each of these approaches has its merits and drawbacks, so it is essential to understand how they work before opting for one.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link