![]() |
VOOZH | about |
Given a dictionary with list as values, extract all the Kth index elements.
Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7], "is" : [9, 3, 8]}, K = 2
Output : [5, 7, 8]
Explanation : The 2nd index elements are 5, 7 and 8 respectively in different keys.Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7], "is" : [9, 3, 8]}, K = 0
Output : [4, 8, 9]
Explanation : The 0th index elements are 4, 8 and 9 respectively in different keys.
Method #1 : Using list comprehension + values()
The combination of above functionalities can be used to solve this problem. In this, the values are extracted using values() and list comprehension is used to construct new list.
The original dictionary is : {'Gfg': [4, 7, 5], 'Best': [8, 6, 7], 'is': [9, 3, 8]}
The extracted values : [7, 6, 3]Time Complexity: O(n), where n is the elements of dictionary
Auxiliary Space: O(n), where n is the size of dictionary
Method #2 : Using map() + itemgetter()
The combination of above functionalities can be used to solve this problem. In this, we use map() to extend logic of getting values of particular key, and itemgetter is used for extracting particular index.
The original dictionary is : {'Gfg': [4, 7, 5], 'Best': [8, 6, 7], 'is': [9, 3, 8]}
The extracted values : [7, 6, 3]Time Complexity: O(n) where n is the number of elements in the list “test_dict”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_dict”.
Method 3: Using a for loop to iterate through the values of the dictionary and append the Kth index element of each value to a new list.
Step-by-step approach:
Below is the implementation of the above approach:
The original dictionary is : {'Gfg': [4, 7, 5], 'Best': [8, 6, 7], 'is': [9, 3, 8]}
The extracted values : [7, 6, 3]Time complexity: O(NM) where N is the number of keys in the dictionary and M is the length of each value list. =
Auxiliary space: O(NM) to store the extracted values list.
Method #4: Using a generator expression
You can also use a generator expression to extract the Kth element of each value in the dictionary.
Steps:
The original dictionary is : {'Gfg': [4, 7, 5], 'Best': [8, 6, 7], 'is': [9, 3, 8]}
The extracted values : [7, 6, 3]Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(1) for the generator expression and O(n) for the resulting list.
Method #5: Using a list comprehension with dictionary items()
Step-by-step approach:
Below is the implementation of the above approach:
The original dictionary is : {'Gfg': [4, 7, 5], 'Best': [8, 6, 7], 'is': [9, 3, 8]}
The extracted values : [7, 6, 3]Time Complexity: O(n), where n is the number of elements in the dictionary, since we need to iterate over each value in the dictionary once.
Auxiliary Space: O(n), where n is the number of elements in the dictionary, since we need to create a new list to store the extracted values.
Method #6: Using dictionary comprehension
The original dictionary is : {'Gfg': [4, 7, 5], 'Best': [8, 6, 7], 'is': [9, 3, 8]}
The extracted values : {'Gfg': 7, 'Best': 6, 'is': 3}Time complexity: O(n), where n is the number of key-value pairs in the dictionary. This is because we need to loop through each key-value pair once.
Auxiliary Space: O(n), where n is the number of elements in the dictionary, since we need to create a new list to store the extracted values.