Python – Extract Kth index elements from Dictionary Value list
Last Updated :
17 Apr, 2023
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
test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]}
print ( "The original dictionary is : " + str (test_dict))
K = 1
res = [sub[K] for sub in test_dict.values()]
print ( "The extracted values : " + str (res))
|
Output
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
from operator import itemgetter
test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]}
print ( "The original dictionary is : " + str (test_dict))
K = 1
res = list ( map (itemgetter(K), test_dict.values()))
print ( "The extracted values : " + str (res))
|
Output
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
test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]}
print ( "The original dictionary is : " + str (test_dict))
K = 1
res = []
for val in test_dict.values():
res.append(val[K])
print ( "The extracted values : " + str (res))
|
Output
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
test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]}
print ( "The original dictionary is : " + str (test_dict))
K = 1
res = (val[K] for val in test_dict.values())
res = list (res)
print ( "The extracted values : " + str (res))
|
Output
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
test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]}
print ( "The original dictionary is : " + str (test_dict))
K = 1
res = [value[K] for key, value in test_dict.items()]
print ( "The extracted values : " + str (res))
|
Output
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
test_dict = { "Gfg" : [ 4 , 7 , 5 ], "Best" : [ 8 , 6 , 7 ], "is" : [ 9 , 3 , 8 ]}
print ( "The original dictionary is : " + str (test_dict))
K = 1
result_dict = {key: value[K] for key, value in test_dict.items()}
print ( "The extracted values : " + str (result_dict))
|
Output
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.
Similar Reads
Python - Filter odd elements from value lists in dictionary
Sometimes, while working with Python dictionaries we can have problem in which we need to perform the removal of odd elements from values list of dictionaries. This can have application in many domains including web development. Lets discuss certain ways in which this task can be performed. Method
6 min read
Python - Extract Key's value from Mixed Dictionaries List
Given a list of dictionaries, with each dictionary having different keys, extract value of key K. Input : test_list = [{"Gfg" : 3, "b" : 7}, {"is" : 5, 'a' : 10}, {"Best" : 9, 'c' : 11}], K = 'b' Output : 7 Explanation : Value of b is 7. Input : test_list = [{"Gfg" : 3, "b" : 7}, {"is" : 5, 'a' : 10
7 min read
Python - Test Kth index in Dictionary value list
Sometimes, while working with Python dictionaries, we can have a problem in which we need to test if, throughout the dictionary, the kth index of all values in the dictionary is equal to N. This kind of problem can occur in the web development domain. Let's discuss certain ways in which this problem
9 min read
Remove Nth Element from Kth key's Value from the Dictionary - Python
We are given a dictionary we need to remove the Nth element from the Kth key value. For example we are given a dictionary d = {'key1': ['a', 'b', 'c', 'd'], 'key2': ['w', 'x', 'y', 'z']} we need to remove the Nth element from the Kth key value so that the output should become {'key1': ['a', 'b', 'd'
3 min read
Python - Key with Maximum element at Kth index in Dictionary Value List
Given a dictionary with values as lists, the task is to write a Python program to get the key with the maximum element at the Kth index by comparing the elements of each list. Input : test_dict = {'Gfg' : [4, 6, 8, 2], 'is' : [1, 4, 5, 9], 'best' :[2, 3, 4, 10], 'for' :[4, 5, 2, 1], 'geeks' :[2, 10,
8 min read
Python - Extract element from list succeeded by K
Given a list, extract the elements which are having K as the next element. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5], K = 3 Output : [2, 5] Explanation : Elements before 3 are 2, 5. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 8], K = 8 Output : [7, 3] Explanation : Elements before 8 are 7, 3. Metho
5 min read
Extract Subset of Key-Value Pairs from Python Dictionary
In this article, we will study different approaches by which we can Extract the Subset Of Key-Value Pairs From the Python Dictionary. When we work with Python dictionaries, it often involves extracting subsets of key-value pairs based on specific criteria. This can be useful for tasks such as filter
4 min read
Python program to extract the Unique Dictionary Value List elements
Given Dictionary with value lists, extract all unique values. Input : test_dict = {"Gfg" : [6, 7, 4, 6], "Geeks" : [6, 8, 5, 2]} Output : [6, 7, 4, 8, 5, 2] Explanation : All distincts elements extracted. Input : test_dict = {"Gfg" : [6, 7, 6], "Geeks" : [6, 8, 5, 2]} Output : [6, 7, 8, 5, 2] Explan
6 min read
Get List of Values From Dictionary - Python
We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3]. Using dict.values()We can use dict.values() along with the list() function to get the list. Here,
2 min read
Python | Extract filtered Dictionary Values
While working with Python dictionaries, there can be cases in which we are just concerned about getting the filtered values list and donât care about keys. This is yet another essential utility and solution to it should be known and discussed. Letâs perform this task through certain methods. Method
4 min read