How to Convert list to dictionary in Python

In this post, we are going to learn some techniques with examples Convert list to dictionary in Python. The list is an ordered collection that can store duplicate data, whereas a dictionary is an unordered collection that can only have unique key pairs with duplicate values. But sometimes we face the problem to convert the list to the dictionary. We represent the list by [] or dictionary by {key: value}

1. Dictionary Comprehension to Convert list to dictionary keys with same value


Dictionary Comprehension allows us to create a new dictionary from the existing dictionary,and run for loop over dictionary elements with a single line of code.

Syntax

dictionary = {key: value for item in iterable} 

Here in the code snippet, we return the new dictionary with all list elements as key, Dictionary Comprehension to loop over each element of the list and adding the same value(12) for all dictionary keys.

#Convert list to the dictionary in Python
animal_list = ['cat','bee','dog','rat']

animal_dict = { animal : 12 for animal in animal_list  }  

print("Converted list to the dictionary in Python:\n",
animal_dict)

Output

Converted list to the dictionary in Python:
 {'cat': 12, 'bee': 12, 'dog': 12, 'rat': 12}

2.How to Convert list to dictionary in Python Using dict.fromKeys()


The dict.fromkeys() allow us to create a new dictionary with keys from iterable elements and values pass by the user.

Syntax

dict.fromKeys(iterable,value)

In this code, iterable is a list(animal_list), its elements are using as keys for new dictionary and we have passed 43 as values for all keys in a dictionary, as we can see in the output, all keys of animal_list have the same value 43.

#Convert list to the dictionary in Python
animal_list = ['cat','bee','dog','rat']

animal_dict = dict.fromkeys(animal_list,43) 

print("Converted list to the dictionary in Python:\n",
animal_dict)

Output

Converted list to the dictionary in Python:
 {'cat': 43, 'bee': 43, 'dog': 43, 'rat': 43}

3.How to Convert list to dictionary in python using counter()


The counter is an unordered collection of data containers and data stored in the counter as an alike dictionary in form of {key: value} pairs with curly({}). counter takes an iterable(list, string literal) as an argument.In the below code we are passing a list as an argument to counter and converting it into the dictionary.

from collections import Counter
animal_list = ['cat','bee','dog','rat']

animal_dict = Counter(animal_list)


print("Converted list to the dictionary in Python:\n",
animal_dict)

Output

Converted list to the dictionary in Python:
 Counter({'cat': 1, 'bee': 1, 'dog': 1, 'rat': 1})

4.How to Convert list to dictionary with enumerated values


In this code snippet, we are creating a new dictionary where list element used as key and value we are creating by a loop over the length of list 0 to len(animal_list)-1 for the value of each key.

#Convert list to the dictionary in Python

animal_list = ['cat','bee','dog','rat']

animal_dict = { item : animal_list[item] for item in range(0, len(animal_list) ) }


print("Converted list to the dictionary in Python:\n",
animal_dict)

Output

Converted list to the dictionary in Python:
 {0: 'cat', 1: 'bee', 2: 'dog', 3: 'rat'}

5.How to Convert list to dictionary Using zip() method


The python zip() built-in function takes iterators as an argument, accumulate them in a tuple, and returns a single iterator .In this code below, we are converting a single list into the dictionary and assigning default values. In the first step, We are creating an iterator from the list and then joining them with the help of zip() into keys and values pair, using dict() method converting into the dictionary.

Syntax

 zip(iterators)

Parameter

  • iterators : The object that contains a countable value. The iterator objects are dictionaries, lists,sets, tuples
#program to convert list to the dictionary in Python

animal_list = ['cat',1,'bee',3,'dog',5,'rat',6]
iterable = iter(animal_list)
animal_dict = dict(zip(iterable, iterable))

print("Converted list to the dictionary in Python:\n",
animal_dict)

Output

Converted list to the dictionary in Python:
 {'cat': 1, 'bee': 3, 'dog': 5, 'rat': 6}

Conclusion

In this post, we have learned different ways to How to Convert list to dictionary in Python. We hope you find it useful and help you solve your problem of converting a list to a dictionary.