Open In App

Convert a List to Dictionary Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a list, write a Python program to convert the given list to a dictionary such that all the odd elements have the key, and even number elements have the value. Since the Python dictionary is unordered, the output can be in any order.

Example

Input :      ['a', 1, 'b', 2, 'c', 3]
Output: {'a': 1, 'b': 2, 'c': 3}
Explaination: In the input we have a list of element which then is conerted into key value pairs of dictonary in the output

Convert a List to Dictionary Python

Below are the methods that we will cover in this article:

Convert a List to a Dictionary using a loop

This method works by initializing an empty dictionary and then iterating through the list in step 2. In each iteration, the key-value pair is added to the dictionary using the current element as the key and the next element as the value. Finally, the dictionary is returned as the result.

Python3




def convert(lst):
   res_dict = {}
   for i in range(0, len(lst), 2):
       res_dict[lst[i]] = lst[i + 1]
   return res_dict
 
lst = ['a', 1, 'b', 2, 'c', 3]
print(convert(lst))


Output

{'a': 1, 'b': 2, 'c': 3}

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n), where n is the length of the input list.

List to Dictionary Conversation using dict Comprehension

To convert a list to dictionary, we can use dict comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type. 

Python3




def Convert(lst):
    res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
    return res_dct
         
# Driver code
lst = ['a', 1, 'b', 2, 'c', 3]
print(Convert(lst))


Output

{'a': 1, 'b': 2, 'c': 3}


Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.

Converting a List to a Dictionary using zip() Method

First create an iterator, and initialize it to variable ‘it’. Then use zip method, to zip keys and values together. Finally typecast it to dict type. 

Python3




def Convert(a):
    it = iter(a)
    res_dct = dict(zip(it, it))
    return res_dct
 
 
# Driver code
lst = ['a', 1, 'b', 2, 'c', 3]
print(Convert(lst))


Output

{'a': 1, 'b': 2, 'c': 3}


Time complexity: O(n), where n is the length of the input list a. 
Auxiliary space: O(n), where n is the length of the input list a. 

List to Dictionary Conversation using Lambda Function

First, create an array of keys and values by using array slicing. Then use the map method with lambda to form array of tuple with key value paris. Finally typecast it to dict type. 

Python




def Convert(lst):
    res_dct = map(lambda i: (lst[i], lst[i+1]), range(len(lst)-1)[::2])
    return dict(res_dct)
 
 
# Driver code
lst = ['a', 1, 'b', 2, 'c', 3]
print(Convert(lst))


Output

{'a': 1, 'c': 3, 'b': 2}


Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.

Converting a List to a Dictionary using List Comprehension and Slicing

Slice the input list to get a list of keys by using lst[::2]. This will take every second element starting from the first element of the list.Slice the input list to get a list of values by using lst[1::2]. This will take every second element starting from the second element of the list. Create a dictionary by using a dictionary comprehension that iterates over the indices of the keys list and pairs each key with its corresponding value from the values list.

Return the resulting dictionary.

Python3




def convert(lst):
    keys = lst[::2# slice the list to get keys
    values = lst[1::2# slice the list to get values
    res_dict = {keys[i]: values[i] for i in range(len(keys))}
    return res_dict
lst = ['a', 1, 'b', 2, 'c', 3]
print(convert(lst))  # {'a': 1, 'b': 2, 'c': 3}


Output

{'a': 1, 'b': 2, 'c': 3}


Time complexity: O(n)
Auxiliary space: O(n).

Convert a List into Dictionary using Itertools

Import the itertools module.Use the zip_longest() function to create a list of tuples from the original list. This function will group the elements into pairs, even if the length of the list is odd.Use a dictionary comprehension to create a dictionary from the list of tuples.Return the dictionary.

Below is the implementation of the above approach:

Python3




import itertools
 
def convert(lst):
    pairs = itertools.zip_longest(*[iter(lst)] * 2, fillvalue=None)
    dct = {key: value for key, value in pairs}
    return dct
 
lst = ['a', 1, 'b', 2, 'c', 3]
print(convert(lst))


Output

{'a': 1, 'b': 2, 'c': 3}


Time complexity: O(n)
Auxiliary space: O(n) (to store the list of tuples)



Last Updated : 18 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads