Let’s check out different ways to convert a list into a dictionary.

Intro

Before jumping into the tutorial, let’s see an example of converting a list into a dictionary. We will have a list of tuples (each tuple consists of two elements) or just a list of elements. We’ll take both input and convert them to dictionaries.

Examples:

In the following example, we have taken a list of tuples and convert it into a dictionary. We have taken the first element of each tuple as a key and the second element as a value for the dictionary. Input: list_one = [(‘a’, ‘A’), (‘b’, ‘B’), (‘c’, ‘C’)] Output: {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’} In the following example, we have taken a list of elements and convert it into a dictionary with alternative elements as key and value. We will give null to the key if there is no element left for it as a key (if the list has an odd number of elements). Input: list_one = [‘a’, ‘A’, ‘b’, ‘B’, ‘c’, ‘C’, ‘d’] Output:{‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: None} We have seen the objective of the tutorial. And we will discuss both the examples in different ways. Let’s start with the first example.

List of Tuples – Dictionary

Let’s see how to convert a list of tuples into a dictionary. You can try to write the code with the help of the following steps.

Initialize the list of tuples with dummy data as given the above examples (make sure each tuple in the list has exactly two elements). Pass the list of tuples to dict method and it stores the result in a new variable. Tha’s it, we have converted a list of tuples into a dictionary with a single line of code.

You can test the above program output by executing it. You will get the result as we see in the examples.

List – Dictionary

We have seen how to convert a list of tuples into a dictionary and it’s a straightforward thing in Python. In this section, we’ll see how to convert a plain list into a dictionary. See the second example from the first section of the tutorial to get more clarity. Follow the below steps to write the code for the second example. We have filled the default value as None for the element that won’t have any value (a list containing an odd number of elements). For that, we need to use a method called zip_longest from the module itertools.

Import the module itertools and initialize a list with an odd number of elements given in the examples. Convert the list to an iterable to avoid repetition of key and value pairs in the zip_longest method. Now, pass the iterable to the method zip_longest and fillvalue as None. It will return an zip object. We have to pass iterable two times as it takes key and value from the two iterables. In our case, both keys and values are in the same iterable. So, we need to pass it two times before fillvalue. Convert the object into the Python dictionary using dict method. Print the result.

You can give the fillvalue whatever you want. Try out different things with it. And execute the code to see whether we are getting the exact output as mentioned in the example or not.

Conclusion

Hope you enjoyed converting the list to a dictionary. Let’s meet in the next tutorial. Next, learn how to Flatten list in Python. Happy Coding 🙂