Convert list to the dictionary in Python

Python List

In this post, we are going to learn some techniques with example to Convert list to the 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. Convert list to the dictionary all keys with the same value.

Using Dictionary Comprehension

Dictionary Comprehension allows us to create a new dictionary from the existing dictionary, 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}

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}

2. Convert list to dictionary using counter()

The counter is an unordered collection of data containers and data store 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})

3.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'}

4. Convert list to dictionary Using zip() method

zip() takes iterators as an argument, accumulate them in a tuple, and return a single iterator.

Syntax

 zip(iterators)

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 then joining them with the help of zip() into keys and values pair, using dict() method converting into the dictionary.

#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 Convert a list to a dictionary in Python. We hope you find it useful and help you solve your problem of converting a list to a dictionary.