Convert lists of key-value in Python Dictionary

Python List

In this post, we are going to understand how to convert lists of key-value in Python Dictionary using different methods with the code examples, The lists of the same length or different length, and many more.

We are using following ways to convert lists of key-value in Python Dictionary

1. Convert lists of same length key-value in Dictionary

In this code snippet, we are converting two lists of the same length into a dictionary using the zip() method.it takes an iterable as an argument and returns an iterator object by joining elements from each iterable., We are joining two lists (iterable) using zip() method, finally, convert a return object into the dictionary using dict() constructor.

Code Example

#program to Convert lists of key-value pair in Dict

lang_lst = ['C#', 'Go','C++', 'Go','Python' ] 
num_lst = [2, 3,4,5,6] 

mydict = dict(zip(lang_lst,num_lst))

print('list to dictionary =\n',mydict)

Output

list to dictionary =
 {'C#': 2, 'Go': 5, 'C++': 4, 'Python': 6}

2. Convert two different length list to dictionary

.zip_longest() is a function defined in the itertools module. It takes iterables as argument and returns an iterator by joining elements from each iterable.

“Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted. Roughly equivalent to:” Python Docs

if iterables are uneven in length then the remaining values of iterables are filled by the values assigned to fillvalue parameter of zip_longest(), if the value for fillvalue parameter is not passed then the padding will be done with ‘None’.

We are zipping the two iterables by using zip_longest(), converting them into the dictionary by using dict() constructor. As we can see we have not provided any value for fillvalue parmeter, therefore remaning value of iterable is padded with ‘none’.

Code Example

#program to Convert lists of key-value pair in Dict

from itertools import zip_longest

lang_lst = ['C#', 'Go','C++', 'Go','Python' ] 
num_lst = [2, 3,4,5] 

mydict = dict(zip_longest(lang_lst,num_lst))
print('list to dictionary =\n',mydict)

Output

list to dictionary =
 {'C#': 2, 'Go': 5, 'C++': 4, 'Python': Non

3. Convert a list of alternative key, values entries to a dictionary

To achieve this we are using list slicing, we have a score_list list that contains data for both subject and marks. To convert into the dictionary, we will create two lists one is subj_list for key and another is marks_list for values from score_list. We will join them using zip() method, finally convert these into a dictionary using dict() constructor.

Code Example

#program to Convert lists of key-value pair in Dict

score_list = ['Math',100, 'Eng',98,'Sci',99, 'Chemi',100 ] 

subj_list = score_list[::2]

marks_list =score_list[1::2]

score_dict = dict(zip(subj_list,marks_list))

print('list to dictionary =\n',score_dict)

Output

list to dictionary =
 {'Math': 100, 'Eng': 98, 'Sci': 99, 'Chemi

 

4. Convert lists of key-values using for loop in dictionaries

In this code snippet, we are using for loop with dictionary comprehension to convert lists of key-value to the dictionary.

Code Example

#program to  Convert List to List of dictionaries
score_list = ['Math',100, 'Eng',98,'Sci',99, 'Chemi',100 ] 
keys_list = ["subject", "score"]

len = len(score_list) 
lst_to_dict = [] 
for ind in range(0, len, 2): 
    lst_to_dict.append({keys_list[0]: score_list[ind], keys_list[1] : score_list[ind + 1]}) 

print('list to dictionary =\n',lst_to_dict)
 

Output

list to dictionary =
 [{'subject': 'Math', 'score': 100}, {'subject': 'Eng', 'score': 98}, {'subject': 'Sci', 'score': 99}, {'subject': 'Chemi', 'score': 100}]

5. Convert lists of key-value in the dictionary

In this code snippet we are using the list and dict comprehension to convert Lists of key-value into dictionary.

Code Example

#program to  Convert List to List of dictionaries
score_list = ['Math',100, 'Eng',98,'Sci',99, 'Chemi',100 ] 
keys_list = ["subject", "score"]

len = len(score_list) 

 
lst_to_dict = [{keys_list[0]: score_list[ind], keys_list[1]: score_list[ind+ 1]}for ind in range(0, len, 2)] 

print('list to dictionary =\n',lst_to_dict)

Output

list to dictionary =
 [{'subject': 'Math', 'score': 100}, {'subject': 'Eng', 'score': 98}, {'subject': 'Sci', 'score': 99}, {'subject': 'Chemi', 'score': 100}]

Conclusion

We have explored different ways to Convert lists of key-value in Python Dictionary with code example. Now by using these methods, We can easily convert lists of key-value in Python Dictionary.

Happy Learning!