Given dictionary with value as lists, slice each list till K.
Input : test_dict = {“Gfg” : [1, 6, 3, 5, 7], “Best” : [5, 4, 2, 8, 9], “is” : [4, 6, 8, 4, 2]}, K = 3
Output : {‘Gfg’: [1, 6, 3], ‘Best’: [5, 4, 2], ‘is’: [4, 6, 8]}
Explanation : The extracted 3 length dictionary value list.Input : test_dict = {“Gfg” : [1, 6, 3, 5, 7], “Best” : [5, 4, 2, 8, 9], “is” : [4, 6, 8, 4, 2]}, K = 2
Output : {‘Gfg’: [1, 6], ‘Best’: [5, 4], ‘is’: [4, 6]}
Explanation : The extracted 2 length dictionary value list.
Method #1 : Using loop + list slicing
The combination of above functionalities can be used to solve this problem. In this, we perform task of list slicing using slice operation and loop to iterate through all the keys.
Python3
# Python3 code to demonstrate working of # Slice till K dictionary value lists # Using loop + list slicing # initializing dictionary test_dict = { "Gfg" : [ 1 , 6 , 3 , 5 , 7 ], "Best" : [ 5 , 4 , 2 , 8 , 9 ], "is" : [ 4 , 6 , 8 , 4 , 2 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 4 res = dict () for sub in test_dict: # slicing till K and reassigning res[sub] = test_dict[sub][:K] # printing result print ( "The dictionary after conversion " + str (res)) |
The original dictionary is : {'Gfg': [1, 6, 3, 5, 7], 'Best': [5, 4, 2, 8, 9], 'is': [4, 6, 8, 4, 2]} The dictionary after conversion {'Gfg': [1, 6, 3, 5], 'Best': [5, 4, 2, 8], 'is': [4, 6, 8, 4]}
Time Complexity: O(n) where n is the number of elements in the dictionary.
Auxiliary Space: O(n), where n is the number of elements in the dictionary.
Method #2 : Using dictionary comprehension + list slicing
This is yet another way in which this task can be performed. In this, we perform task of reassigning using dictionary comprehension and provides one liner approach to this problem.
Python3
# Python3 code to demonstrate working of # Slice till K dictionary value lists # Using dictionary comprehension + list slicing # initializing dictionary test_dict = { "Gfg" : [ 1 , 6 , 3 , 5 , 7 ], "Best" : [ 5 , 4 , 2 , 8 , 9 ], "is" : [ 4 , 6 , 8 , 4 , 2 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 4 # one-liner to slice elements of list # items() used to get all key-value pair of dictionary res = {key: val[:K] for key, val in test_dict.items()} # printing result print ( "The dictionary after conversion " + str (res)) |
The original dictionary is : {'Gfg': [1, 6, 3, 5, 7], 'Best': [5, 4, 2, 8, 9], 'is': [4, 6, 8, 4, 2]} The dictionary after conversion {'Gfg': [1, 6, 3, 5], 'Best': [5, 4, 2, 8], 'is': [4, 6, 8, 4]}
The Space and Time Complexity is the same for both methods:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#3: Using Recursive method.
Algorithm:
- Define a function slice_dict that takes two arguments, a dictionary d and an integer k.
- If the input dictionary is a dictionary, recursively iterate over its key-value pairs and slice the value list until the first k elements. Return a new dictionary with the sliced values.
- If the input dictionary is a list, slice the list until the first k elements and return the sliced list.
- If the input is not a dictionary or a list, return the input as is.
- Initialize the input dictionary and integer.
- Call the slice_dict function with the input dictionary and integer.
- Print the original and the converted dictionary.
Python3
def slice_dict(d, k): if isinstance (d, dict ): return {key: slice_dict(value, k) for key, value in d.items()} elif isinstance (d, list ): return d[:k] else : return d test_dict = { "Gfg" : [ 1 , 6 , 3 , 5 , 7 ], "Best" : [ 5 , 4 , 2 , 8 , 9 ], "is" : [ 4 , 6 , 8 , 4 , 2 ]} K = 4 res = slice_dict(test_dict, K) print ( "The original dictionary is : " + str (test_dict)) print ( "The dictionary after conversion " + str (res)) |
The original dictionary is : {'Gfg': [1, 6, 3, 5, 7], 'Best': [5, 4, 2, 8, 9], 'is': [4, 6, 8, 4, 2]} The dictionary after conversion {'Gfg': [1, 6, 3, 5], 'Best': [5, 4, 2, 8], 'is': [4, 6, 8, 4]}
Time Complexity: O(n * k), where n is the number of keys in the dictionary and k is the integer input.
The function iterates over n key-value pairs, each of which contains a list of length m.
Slicing the list to k elements takes O(k) time. Thus, the total time complexity is O(n * k).
Space Complexity: O(n * m), where n is the number of keys in the dictionary and m is the maximum length of the lists in the dictionary.
The function creates a new dictionary with sliced lists, which can contain up to n lists of length m.
Therefore, the space complexity is O(n * m).