Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Incremental value initialization in Dictionary

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 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




# Python3 code to demonstrate
# Incremental value initialization in Dictionary
# using Dictionary comprehension + enumerate()
 
# initializing list
test_list = ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialization K
K = 4
 
# using Dictionary comprehension + enumerate()
# Incremental value initialization in Dictionary
res = {val : (K * (idx + 1)) for idx, val in enumerate(test_list)}
 
# print result
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




# Python3 code to demonstrate
# Incremental value initialization in Dictionary
# using dict() + zip()
 
# initializing list
test_list = ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialization K
K = 4
 
# using dict() + zip()
# Incremental value initialization in Dictionary
res = dict(zip(test_list, range(K, len(test_list) * (K + 1), K)))
 
# print result
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




# Python3 code to demonstrate
# Incremental value initialization in Dictionary
# using for loop
   
# initializing list
test_list = ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
   
# printing original list
print("The original list : " + str(test_list))
   
# initialization K
K = 4
   
# using for loop
# Incremental value initialization in Dictionary
res = {}
for i, val in enumerate(test_list):
    res[val] = K * (i + 1)
   
# print result
print("The Dictionary after index keys : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




# Python3 code to demonstrate
# Incremental value initialization in Dictionary
# using for loop
 
# initializing list
test_list = ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialization K
K = 4
 
# using for loop
# Incremental value initialization in Dictionary
res=dict()
for i in range(0,len(test_list)):
    res[test_list[i]]=K*(i+1)
# print result
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
 
# Define the list of values to use as keys
test_list = ['Nikhil', 'Akshat', 'Akash', 'Manjeet']
 
# Define the increment value
K = 4
 
# Create a defaultdict with int type as the default value
res = defaultdict(int)
 
# Loop through the list of keys and their indices
for i, val in enumerate(test_list):
     
    # Assign the key and its corresponding value to the defaultdict
    res[val] = K*(i+1)
 
# Convert the defaultdict to a standard dictionary and print the result
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments