Python | Initializing dictionary with list index-values
Last Updated :
22 Apr, 2023
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
# Python3 code to demonstrate working of
# Initializing dictionary with list index-values
# Using dictionary comprehension + len()
# initializing list
test_list = ['Gfg', 'is', 'best']
# printing original list
print("The original list is : " + str(test_list))
# Initializing dictionary with list index-values
# Using dictionary comprehension + len()
res = {x : test_list[x] for x in range(len(test_list))}
# printing result
print("The dictionary indexed as list is : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Initializing dictionary with list index-values
# Using dict() + enumerate()
# initializing list
test_list = ['Gfg', 'is', 'best']
# printing original list
print("The original list is : " + str(test_list))
# Initializing dictionary with list index-values
# Using dict() + enumerate()
res = dict(enumerate(test_list))
# printing result
print("The dictionary indexed as list is : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Initializing dictionary with list index-values
# Using zip() and dict()
# initializing list
test_list = ['Gfg', 'is', 'best']
# printing original list
print("The original list is : " + str(test_list))
# Initializing dictionary with list index-values
# Using zip() and dict()
res = dict(zip(range(len(test_list)), test_list))
# printing result
print("The dictionary indexed as list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe 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
# initializing list
test_list = ['Gfg', 'is', 'best']
# printing original list
print("The original list is : " + str(test_list))
# Initializing dictionary with list index-values
# Using for loop
res = {}
for i in range(len(test_list)):
res[i] = test_list[i]
# printing result
print("The dictionary indexed as list is : " + str(res))
OutputThe 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.
Similar Reads
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 per
5 min read
Python - Initialize dictionary with custom value list
In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list corresponding to key when it comes. But its always wanted a method to initialize the dict. keys with a custom list.
4 min read
Python | Initialize dictionary with None values
Sometimes, while working with dictionaries, we might have a utility in which we need to initialize a dictionary with None values so that they can be altered later. This kind of application can occur in cases of memoization in general or competitive programming. Let's discuss certain ways in which th
4 min read
Initialize Python Dictionary with Keys and Values
In this article, we will explore various methods for initializing Python dictionaries with keys and values. Initializing a dictionary is a fundamental operation in Python, and understanding different approaches can enhance your coding efficiency. We will discuss common techniques used to initialize
3 min read
Python | Initialize list with empty dictionaries
While working with Python, we can have a problem in which we need to initialize a list of a particular size with empty dictionaries. This task has it's utility in web development to store records. Let's discuss certain ways in which this task can be performed. Method #1 : Using {} + "*" operator Thi
5 min read
Python | Initialize dictionary with common value
While working with Python, sometimes, we might have a problem in which we require the initialize the static list into a dictionary with a constant value. Let's discuss certain ways in which this task can be performed. Method #1: Using dict() + list comprehension The combination of above functions ca
5 min read
Python - Dictionary with Index as Value
We are having a list we need to find index of each element and store it in form of dictionary. For example, a = ['a', 'b', 'c', 'd'] we need to find index of each elements in list so that output should be {'a': 0, 'b': 1, 'c': 2, 'd': 3}.Using Dictionary ComprehensionWe can use dictionary comprehens
2 min read
Python | Grouping list values into dictionary
Sometimes, while working with data, we can be encountered a situation in which we have a list of lists and we need to group its 2nd index with the common initial element in lists. Let's discuss ways in which this problem can be solved. Method 1: Using defaultdict() + loop + dict() The defaultdict ca
7 min read
Python - Custom dictionary initialization in list
While working with Python, we can have a problem in which we need to initialize a list of a particular size with custom dictionaries. This task has itâs utility in web development to store records. Letâs discuss certain ways in which this task can be performed. Method #1 : Using {dict} + "*" operato
3 min read
Python - Incremental value initialization in Dictionary
The interconversion between the datatypes is very popular and hence many articles have been written to demonstrate different kind of problems with their solutions. This article deals with yet another similar type problem of converting a list to dictionary, with values as the index incremental with K
5 min read