In this article, MarsDevs will present steps that help you convert a Python List to a Dictionary using different methods or processes.
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
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.
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:
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.
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:
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
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
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
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
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
Now here we will use counter() method present in the collection module of python for converting a list to a dictionary.
Steps involved