![]() |
VOOZH | about |
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.
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.
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.
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()
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:
{'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.