Convert List to Dictionary in Python

In this article, MarsDevs will present steps that help you convert a Python List to a Dictionary using different methods or processes.

Convert List to Dictionary in Python

List and Dictionary are both widely used data types in Python. In a List, data is stored in the form of an array, whereas in a Dictionary, the data is stored in the form of key-value pairs. 

There are different ways of converting a list to a dictionary in python, like using the counter(), zip(), and enumerate() methods.

Now let's discuss why there is a need to convert a list to a dictionary

  • Suppose we have a list of subjects opted for by a class, and we need to count the frequency of each subject appearing in the list. We can fulfill this use case by converting this list to a dictionary.
  • Now let’s take another use case. We have one list containing alphabets and the second list containing their ASCII values. Our use case is to map the alphabet against their ASCII values. In this case, we need to convert the lists into a dictionary.

Now, let's discuss some examples of lists and dictionaries to get a better understanding of both topics.

We are going to learn about the different methods used for converting a list into a dictionary.

(i) Using Dictionary Comprehension

Dictionary Comprehension is a very powerful method, used for converting one dictionary to another. While performing this comprehension i.e., transforming one dictionary to another, the data can be included or excluded, conditionally, into the new dictionary.

Let’s look at the example of dictionary comprehension

Explanation: 

  • Line 1 - Here we take the input for n
  • Line 2 -  Here using dictionary comprehension, we created a dictionary having numbers as its keys and their cube as its value
  • Line 3 - In this, we printed the dictionary

Output: 

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

But our main task is to convert the list to a dictionary, so now we are using dictionary comprehension for the same.

In the above example, we can see that the second element is the square of the first element and so on.

(ii) Using zip() method

This method creates a zip (where the first element passed in each iterator is paired with a second element passed in each iterator). This method takes an iterator (list, tuple, dictionary object) object as the parameter. If the length of iterators varies, then the iterator with a small length will decide the length of the resultant object. We will iterate with min length iterate and will discard the remaining values from the larger list.

Syntax: 

(iii) Using dict.fromkeys() method

The method dict.fromkeys() returns the keys mapped with their specific values. The method takes sequence(list) and value (which need to be mapped with the sequence) as the input & returns the dictionary. If no value input is given to dict.fromKeys() method then it will return a dictionary with values as ‘None’.

Syntax:

dict.fromkeys(sequence, val)

Steps involved

  • Create a sequence(list) that contains data.
  • Now pass the second parameter to the dict.fromKeys() method.
  • Now call the method and it will return the required dictionary.

(iv) Using Tuples

It is the simplest way of converting a list to a dictionary. In this process, we will create a list of tuples, each tuple containing (key-value pair). Later on, this can be converted into a dictionary using the type cast method. 

Steps involved

  • Creating a list of tuples and initializing them.
  • Create an empty dictionary.
  • Converting that list to a dictionary using type casting.

(v) Converting list having alternate keys, and values to Dictionary

This method is a bit confusing. In this, first, we will create a list of alternate key-value pairs, then match the first key to the third value, the second key to the fourth value, and so on. We can even say this is a matching of even-even elements with odd-odd elements.

Steps involved

  • Create a list having alternate key-value pairs.
  • Now slicing the list to achieve the task.
  • Now we will use dictionary comprehension.

(vi) Converting a list of dictionaries to a Dictionary

As the name suggests, in this process we have a list of dictionaries and we will combine all these dictionaries to form one single dictionary. 

Steps involved

  • Creating a list of dictionaries.
  • Using dictionary comprehension, we will convert them to a single Dictionary.

(vii) Using enumerate() method

The enumerate() method adds a counter to the iterators and returns an enumerator object. This method can be directly used in loops for converting list to Dictionary. 

Syntax : 

Iterable can be any iterator object like list, dict, etc. The start is the index value from which the counter needs to be started.

Steps involved

  • Create a list, it can be of any type.
  • Now call the enumerate() method by passing the list created in the parameter along with the index value of the counter.
  • We will get an enumerator object now and type cast it into the dictionary.
  • Then print the resultant dictionary.

(viii) Using counter() method

Now here we will use counter() method present in the collection module of python for converting a list to a dictionary.

Steps involved

  • Create a sample list.
  • Now from the collection module import counter() method.
  • Then type cast the result to the dictionary.