Given Dictionary, resize keys to K by getting starting k elements from keys.
Input : test_dict = {“neveropen” :3, “best” :3, “coding” :4, “practice” :3}, K = 3
Output : {‘gee’: 3, ‘bes’: 3, ‘cod’: 4, ‘pra’: 3}
Explanation : Keys resized to have 3 elements.Input : test_dict = {“neveropen” :3, “best” :3, “coding” :4, “practice” :3}, K = 4
Output : {‘geek’: 3, ‘best’: 3, ‘codi’: 4, ‘prac’: 3}
Explanation : Keys resized to have 4 elements.
Method #1 : Using slicing + loop
In this, resizing is done using slicing of dictionary keys, loop is used to iterate for all the keys of dictionary.
Python3
# Python3 code to demonstrate working of # Resize Keys in dictionary # Using slicing + loop # initializing dictionary test_dict = { "neveropen" : 3 , "best" : 3 , "coding" : 4 , "practice" : 3 } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 2 # reforming dictionary res = dict () for key in test_dict: # resizing to K prefix keys res[key[:K]] = test_dict[key] # printing result print ( "The required result : " + str (res)) |
Output:
The original dictionary is : {‘neveropen’: 3, ‘best’: 3, ‘coding’: 4, ‘practice’: 3} The required result : {‘ge’: 3, ‘be’: 3, ‘co’: 4, ‘pr’: 3}
Time Complexity: O(N*K), where N is the number of elements in the dictionary and K is the length of the each dictionary.
Auxiliary Space: O(N)
Method #2 : Using dictionary comprehension + slicing
In this, we perform task of reforming dictionary in one liner using dictionary comprehension.
Python3
# Python3 code to demonstrate working of # Resize Keys in dictionary # Using dictionary comprehension + slicing # initializing dictionary test_dict = { "neveropen" : 3 , "best" : 3 , "coding" : 4 , "practice" : 3 } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 2 # reforming dictionary res = {key[:K]: test_dict[key] for key in test_dict} # printing result print ( "The required result : " + str (res)) |
Output:
The original dictionary is : {‘neveropen’: 3, ‘best’: 3, ‘coding’: 4, ‘practice’: 3} The required result : {‘ge’: 3, ‘be’: 3, ‘co’: 4, ‘pr’: 3}
Method #3: Using map() function with lambda function
- Initialize a lambda function that takes in a key-value pair from the original dictionary and slices the first K characters of the key to create a new key.
- Use the map() function to apply the lambda function to each key-value pair in the original dictionary.
- Convert the resulting map object to a dictionary using the dict() constructor to create the new dictionary with resized keys and corresponding values.
Python3
# Python3 code to demonstrate working of # Resize Keys in dictionary # Using map() + lambda # initializing dictionary test_dict = { "neveropen" : 3 , "best" : 3 , "coding" : 4 , "practice" : 3 } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 2 # reforming dictionary res = dict ( map ( lambda x: (x[ 0 ][:K], x[ 1 ]), test_dict.items())) # printing result print ( "The required result : " + str (res)) |
The original dictionary is : {'neveropen': 3, 'best': 3, 'coding': 4, 'practice': 3} The required result : {'ge': 3, 'be': 3, 'co': 4, 'pr': 3}
Time complexity: O(N), where N is the number of key-value pairs in the dictionary.
Auxiliary space: O(N), to store the new dictionary with resized keys and corresponding values.