Open In App

Python | Initializing dictionary with list index values

Improve
Improve
Like Article
Like
Save
Share
Report

While working with dictionaries, we might come across a problem in which we require to attach each value in list with it’s index, to be used afterwards to solve question. This technique is usually very useful in competitive programming domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using dictionary comprehension and enumerate() The combo of above methods can achieve this task. In this, enumerate()’s inbuilt capability to iterate value with it’s index is used to construct a key to corresponding value using dictionary comprehension. 

Python3




# Python3 code to demonstrate working of
# Initializing dictionary with index value
# Using dictionary comprehension and enumerate()
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS']
 
# Printing original list
print("The original list is : " + str(test_list))
 
# using dictionary comprehension and enumerate()
# Initializing dictionary with index value
res = {key: val for val, key in enumerate(test_list)}
 
# printing result
print("Constructed dictionary with index value : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best', 'for', 'CS']
Constructed dictionary with index value : {'gfg': 0, 'is': 1, 'best': 2, 'CS': 4, 'for': 3}

Time complexity: O(n), where n is the length of the input list ‘test_list’.
Auxiliary space: O(n), as we are creating a dictionary with n key-value pairs.

Method #2 : Using zip() + dict() + range() + len() This task can also be performed by nesting the above functions. The task performed above by enumerate is handles by range and len functions and zip and dict perform the task of binding key with value and dictionary conversion respectively. 

Python3




# Python3 code to demonstrate working of
# Initializing dictionary with index value
# Using zip() + dict() + range() + len()
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS']
 
# Printing original list
print("The original list is : " + str(test_list))
 
# using zip() + dict() + range() + len()
# Initializing dictionary with index value
res = dict(zip(test_list, range(len(test_list))))
 
# printing result
print("Constructed dictionary with index value : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best', 'for', 'CS']
Constructed dictionary with index value : {'gfg': 0, 'is': 1, 'best': 2, 'CS': 4, 'for': 3}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using a iteration and dict.update()

Python3




# Python3 code to demonstrate working of
# Initializing dictionary with index value
# Using dict.update()
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS']
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing empty dictionary
res = {}
 
# Using dict.update()
for i in range(len(test_list)):
    res.update({test_list[i]: i})
 
# printing result
print("Constructed dictionary with index value : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : ['gfg', 'is', 'best', 'for', 'CS']
Constructed dictionary with index value : {'gfg': 0, 'is': 1, 'best': 2, 'for': 3, 'CS': 4}

The above code snippet creates an empty dictionary, then uses a for loop to iterate over the range of the length of the input list. For each iteration, it updates the dictionary by adding the current element of the list as the key and the current index as the value, using the dict.update() method.

Time Complexity: O(n) where n is the number of elements in the list, as we are iterating through the list once
Auxiliary Space: O(n) as we are creating a new dictionary with n key-value pairs.

Method #4 : Using for loop

Python3




# Python3 code to demonstrate working of
# Initializing dictionary with index value
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS']
 
# Printing original list
print("The original list is : " + str(test_list))
 
 
# Initializing dictionary with index value
res=dict()
for i in range(0,len(test_list)):
    res[test_list[i]]=i
 
# printing result
print("Constructed dictionary with index value : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'CS']
Constructed dictionary with index value : {'gfg': 0, 'is': 1, 'best': 2, 'for': 3, 'CS': 4}

Time Complexity: O(n) where n is the number of elements in the list, as we are iterating through the list once
Auxiliary Space: O(n) as we are creating a new dictionary with n key-value pairs.

Method #6: Using map() and dict() functions in Python

This method uses the map() function to generate a sequence of index values and the dict() function to create a dictionary with index values.

Step-by-step approach:

Initialize a list test_list.
Use the enumerate() function to generate a sequence of index-value pairs from test_list.
Use the map() function with reversed() to swap the order of each index-value pair.
Use the dict() function to create a dictionary with the index-value pairs.
Store the result in a dictionary res.
Print the result.

Python3




# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'CS']
 
# Using map() and dict() functions
res = dict(map(reversed, enumerate(test_list)))
 
# Print the result
print("Constructed dictionary with index value : " + str(res))


Output

Constructed dictionary with index value : {'gfg': 0, 'is': 1, 'best': 2, 'for': 3, 'CS': 4}

Time Complexity: O(n)
Auxiliary Space: O(n)



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