Open In App

Python program to Convert a list into a dictionary with index as key

Given a python list, the task is to write a Python program to convert the list into a dictionary with indexes as keys.

Example:



Input – [‘Ramesh’, ‘Mahesh’, ‘Kamlesh’, ‘Suresh’]
Output – {0: ‘Ramesh’, 1: ‘Mahesh’, 2: ‘Kamlesh’, 3: ‘Suresh’}

Input – [‘Carrot’, ‘Raddish’, ‘Brinjal’, ‘Potato’]
Output – {0: ‘Carrot’, 1: ‘Raddish’, 2: ‘Brinjal’, 3: ‘Potato’}



Convert a list into a dictionary Using Dictionary Comprehensions

In this method without using any extra function, we will convert a list into a dictionary.




def list_to_dict_with_index(lst):
    dict_with_index = {index: value for index, value in enumerate(lst)}
    return dict_with_index
 
if __name__ == "__main__":
    sample_list = ['a', 'b', 'c', 'd', 'e']
    result_dict = list_to_dict_with_index(sample_list)
    print(result_dict)

Output:

{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}

Convert a list into a dictionary Using enumerates and a for loop.

In this method, we will enumerate the provided list and then use the index as a key and assign it to the corresponding value.




employee_names = ['Ramesh', 'Mahesh', 'Kamlesh', 'Suresh']
 
names_dict = {}
for i, name in enumerate(employee_names):
  names_dict[i] = name
 
print(names_dict)

Output
{0: 'Ramesh', 1: 'Mahesh', 2: 'Kamlesh', 3: 'Suresh'}

Time complexity: O(n), where n is the number of elements in the employee_names list. 
Auxiliary space: O(n), as the names_dict dictionary has to store one key-value pair for each element in the employee_names.

The above task can be done without using the for loop as shown below:




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

Output
{0: 'Carrot', 1: 'Raddish', 2: 'Brinjal', 3: 'Potato'}

Time complexity: O(n), where n is the number of elements in the vegetables list. 
Auxiliary space: O(n), where n is the number of elements in the vegetables list.

Convert a list into a dictionary Using the zip and range function

In this method, basically, we use lists of the values and the list of indexes that are created by using the range() function.




vegetables = ['Carrot', 'Raddish', 'Brinjal', 'Potato']
 
veg_dict = dict(zip(range(len(vegetables)), vegetables))
print(veg_dict)

Output
{0: 'Carrot', 1: 'Raddish', 2: 'Brinjal', 3: 'Potato'}

Time complexity: O(n), where n is the length of the list ‘vegetables’.
Auxiliary space: O(n), as the dictionary ‘veg_dict’ created will have n key-value pairs, where n is the length of the list ‘vegetables’.

Convert a list into a dictionary using yield

Use the dict function and pass it a generator function that generates the tuples. This can be done using a generator function and the yield function.




def index_tuple_generator(lst):
    for i, x in enumerate(lst):
        yield (i, x)
 
lst = ['Ramesh', 'Mahesh', 'Kamlesh', 'Suresh']
 
dct = dict(index_tuple_generator(lst))
print(dct)
#This code is contributed by Edula Vinay Kumar Reddy

Output
{0: 'Ramesh', 1: 'Mahesh', 2: 'Kamlesh', 3: 'Suresh'}

Time complexity: O(n) because it involves a single loop that iterates through all the elements in the input list.
Auxiliary Space: O(1) because it does not use any additional data structures and the space it uses is independent of the input size. The yield statement is used to generate the tuples one at a time, so the function does not need to store all the tuples in memory at once.

Convert a list into a dictionary Using list comprehension and the enumerate function.

Use a list comprehension and the built-in enumerate function to generate a list of tuples containing the index and element of each item in the input list. Then, the built-in dict function is used to convert the list of tuples into a dictionary.

Follow the below steps to implement the above idea:

Below is the implementation of the above approach:




def index_tuple_generator(lst):
    return dict([(i, x) for i, x in enumerate(lst)])
 
lst = ['Ramesh', 'Mahesh', 'Kamlesh', 'Suresh']
dct = index_tuple_generator(lst)
print(dct)

Output
{0: 'Ramesh', 1: 'Mahesh', 2: 'Kamlesh', 3: 'Suresh'}

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.


Article Tags :