Python | Initializing dictionary with list index values
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'CS' ]
print ("The original list is : " + str (test_list))
res = {key: val for val, key in enumerate (test_list)}
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'CS' ]
print ("The original list is : " + str (test_list))
res = dict ( zip (test_list, range ( len (test_list))))
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'CS' ]
print ( "The original list is : " + str (test_list))
res = {}
for i in range ( len (test_list)):
res.update({test_list[i]: i})
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}
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'CS' ]
print ( "The original list is : " + str (test_list))
res = dict ()
for i in range ( 0 , len (test_list)):
res[test_list[i]] = i
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'CS' ]
res = dict ( map ( reversed , enumerate (test_list)))
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
Share your thoughts in the comments
Please Login to comment...