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.
Python3
# Python3 code to demonstrate working of # Extract Kth index elements from Dictionary Value list # Using list comprehension + values() # initializing dictionary test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 1 # one liner, values() getting all value according to keys res = [sub[K] for sub in test_dict.values()] # printing result print ( "The extracted values : " + str (res)) |
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.
Python3
# Python3 code to demonstrate working of # Extract Kth index elements from Dictionary Value list # Using map() + itemgetter() from operator import itemgetter # initializing dictionary test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 1 # map and itemgetter() extracting result # list() used to convert result from map() to list format res = list ( map (itemgetter(K), test_dict.values())) # printing result print ( "The extracted values : " + str (res)) |
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:
- Initialize an empty list to store the Kth index elements.
- Iterate through the values of the dictionary using a for loop.
- Access the Kth index element of each value using indexing and append it to the list initialized in step 1.
- Print the extracted values.
Below is the implementation of the above approach:
Python3
# initializing dictionary test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 1 # initializing empty list to store Kth index elements res = [] # iterating through values of dictionary for val in test_dict.values(): # appending Kth index element to the list res.append(val[K]) # printing result print ( "The extracted values : " + str (res)) |
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:
- Define a generator expression that iterates through the values of the dictionary and yields the Kth element of each value.
- Convert the generator expression to a list to get the extracted values.
Python3
# initializing dictionary test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 1 # using generator expression to extract Kth element of each value res = (val[K] for val in test_dict.values()) # converting generator expression to list res = list (res) # printing result print ( "The extracted values : " + str (res)) |
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:
- It initializes an integer variable K with the value 1.
- It uses a list comprehension with the items() method of the dictionary to extract the Kth element of each value in the dictionary.
- The list comprehension iterates through the dictionary items and extracts the Kth element from each value using indexing.
- It assigns the resulting list to the variable res.
- Finally, it prints the extracted values using the print() function and string concatenation.
Below is the implementation of the above approach:
Python3
# initializing dictionary test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 1 # using list comprehension with dictionary items() to extract Kth element of each value res = [value[K] for key, value in test_dict.items()] # printing result print ( "The extracted values : " + str (res)) |
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
- Initialize an empty dictionary result_dict.
- Loop through each key-value pair in the test_dict dictionary using a for loop.
- Use the K value to extract the Kth element from the value list of each key.
- Add the key and extracted value to the result_dict dictionary using dictionary comprehension.
- Return the result_dict.
Python3
# initializing dictionary test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing K K = 1 # using dictionary comprehension to extract Kth element of each value result_dict = {key: value[K] for key, value in test_dict.items()} # printing result print ( "The extracted values : " + str (result_dict)) |
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.