Python | Initializing dictionary with list index-values
While working with Python we might need to perform tasks in which we need to assign a dictionary with list values as dictionary values and index as dictionary keys. This type of problem is quite common in cases we need to perform data-type conversion. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using dictionary comprehension + len() This task can be performed using the combination of above functions in which we perform the construction of dictionary using the dictionary comprehension and indexing the limited using the len function.
Python3
test_list = [ 'Gfg' , 'is' , 'best' ]
print ( "The original list is : " + str (test_list))
res = {x : test_list[x] for x in range ( len (test_list))}
print ( "The dictionary indexed as list is : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best']
The dictionary indexed as list is : {0: 'Gfg', 1: 'is', 2: 'best'}
Time complexity: O(n), where n is the length of the input list, as the code needs to traverse the entire input list to create the dictionary.
Auxiliary space: O(n), as the size of the output dictionary is proportional to the size of the input list.
Method #2 : Using dict() + enumerate() The combination of these methods can also be used to perform this task. In this we use the quality of enumerate function to get the indices and dict() is used to convert the list to dictionary.
Python3
test_list = [ 'Gfg' , 'is' , 'best' ]
print ( "The original list is : " + str (test_list))
res = dict ( enumerate (test_list))
print ( "The dictionary indexed as list is : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best']
The dictionary indexed as list is : {0: 'Gfg', 1: 'is', 2: 'best'}
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.
Method #3: Using zip() and dict()
This approach uses the zip() function to combine the list elements with their indices and the dict() function to convert the resulting tuples to a dictionary.
Python3
test_list = [ 'Gfg' , 'is' , 'best' ]
print ( "The original list is : " + str (test_list))
res = dict ( zip ( range ( len (test_list)), test_list))
print ( "The dictionary indexed as list is : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best']
The dictionary indexed as list is : {0: 'Gfg', 1: 'is', 2: 'best'}
Time complexity: O(n)
Auxiliary space: O(n)
Method #4: Using a for loop
Step-by-Step Approach:
Initialize the list ‘test_list’ with some values.
Print the original list using print() function.
Initialize an empty dictionary ‘res’.
Use a for loop to iterate over the range of length of ‘test_list’.
Inside the for loop, use the index ‘i’ to access the elements of ‘test_list’ at index i and assign it to the corresponding key i in ‘res’.
Print the final dictionary using print() function.
Python3
test_list = [ 'Gfg' , 'is' , 'best' ]
print ( "The original list is : " + str (test_list))
res = {}
for i in range ( len (test_list)):
res[i] = test_list[i]
print ( "The dictionary indexed as list is : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best']
The dictionary indexed as list is : {0: 'Gfg', 1: 'is', 2: 'best'}
Time Complexity: O(n) – since we are iterating over the list only once.
Auxiliary Space: O(n) – since we are creating a new dictionary with n key-value pairs.
Last Updated :
22 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...