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 difference. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using dictionary comprehension + enumerate() This problem can be solved easily using the combination of above functions, dictionary comprehension can perform the task of constructing the dictionary and enumerate function can be used to access the index value along with the element.
Python3
test_list = [ 'Nikhil' , 'Akshat' , 'Akash' , 'Manjeet' ]
print ("The original list : " + str (test_list))
K = 4
res = {val : (K * (idx + 1 )) for idx, val in enumerate (test_list)}
print ("The Dictionary after index keys : " + str (res))
|
Output :
The original list : ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
The Dictionary after index keys : {'Akash': 12, 'Nikhil': 4, 'Manjeet': 16, 'Akshat': 8}
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the dictionary comprehension + enumerate which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using dict() + zip() This problem can also be solved using the combination of above 2 functions, the dict method can be used to convert to dictionary and zip function can be used to map the indices with the keys.
Python3
test_list = [ 'Nikhil' , 'Akshat' , 'Akash' , 'Manjeet' ]
print ("The original list : " + str (test_list))
K = 4
res = dict ( zip (test_list, range (K, len (test_list) * (K + 1 ), K)))
print ("The Dictionary after index keys : " + str (res))
|
Output :
The original list : ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
The Dictionary after index keys : {'Akash': 12, 'Nikhil': 4, 'Manjeet': 16, 'Akshat': 8}
Time Complexity: O(n), where n is the values in dictionary
Auxiliary Space: O(n), where n is the size of dictionary
Method #3 : Using for loop
This method uses a for loop to iterate through the list and add key-value pairs to the dictionary.
Python3
test_list = [ 'Nikhil' , 'Akshat' , 'Akash' , 'Manjeet' ]
print ( "The original list : " + str (test_list))
K = 4
res = {}
for i, val in enumerate (test_list):
res[val] = K * (i + 1 )
print ( "The Dictionary after index keys : " + str (res))
|
Output
The original list : ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
The Dictionary after index keys : {'Nikhil': 4, 'Akshat': 8, 'Akash': 12, 'Manjeet': 16}
Time complexity: O(n), where n is the number of elements in the list
Auxiliary Space: O(n), to store the dictionary of n key-value pairs
Method #4 : Using for loop + dict()
Python3
test_list = [ 'Nikhil' , 'Akshat' , 'Akash' , 'Manjeet' ]
print ( "The original list : " + str (test_list))
K = 4
res = dict ()
for i in range ( 0 , len (test_list)):
res[test_list[i]] = K * (i + 1 )
print ( "The Dictionary after index keys : " + str (res))
|
Output
The original list : ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
The Dictionary after index keys : {'Nikhil': 4, 'Akshat': 8, 'Akash': 12, 'Manjeet': 16}
Time complexity: O(n), where n is the number of elements in the list
Auxiliary Space: O(n), to store the dictionary of n key-value pairs
Method 5: Using a defaultdict and a for loop
We can uses the defaultdict class from the collections module to create a dictionary with a default value of 0 for any keys that haven’t been added yet. The enumerate() function is used to iterate over the list of keys and their indices. The value for each key is then calculated using the provided formula (K*(i+1)) and assigned to the defaultdict. Finally, the dict() constructor is used to convert the defaultdict to a standard dictionary, and the result is printed.
Below is the implementation:
Python3
from collections import defaultdict
test_list = [ 'Nikhil' , 'Akshat' , 'Akash' , 'Manjeet' ]
K = 4
res = defaultdict( int )
for i, val in enumerate (test_list):
res[val] = K * (i + 1 )
print ( dict (res))
|
Output
{'Nikhil': 4, 'Akshat': 8, 'Akash': 12, 'Manjeet': 16}
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), where n is the length of the input list test_list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Apr, 2023
Like Article
Save Article