Converting a list to the dictionary

This method is used to convert a python list into a python dictionary with indexes as keys.

Python3




vegetables = ['Carrot', 'Raddish',
              'Brinjal', 'Potato']
 
veg_dict = dict(enumerate(vegetables))
print(veg_dict)


Output:

{0: ‘Carrot’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’}

There are other methods as well to add values to a dictionary but the above-shown methods are some of the most commonly used ones and highly efficient as well.

How to add values to dictionary in Python

In this article, we will learn what are the different ways to add values in a dictionary in Python

Similar Reads

Assign values using unique keys

After defining a dictionary we can index through it using a key and assign a value to it to make a key-value pair....

Merging two dictionaries using update()

...

Add values to dictionary Using two lists of the same length

...

Converting a list to the dictionary

We can merge two dictionaries by using the update() function....

Add values to dictionary Using the merge( | ) operator

...

Add values to dictionary Using the in-place merge( |= ) operator.

This method is used if we have two lists and we want to convert them into a dictionary with one being key and the other one as corresponding values....

Contact Us