Python - Extract ith element of K key's value
Last Updated :
12 Apr, 2023
Given a dictionary, extract ith element of K key's value list.
Input : test_dict = {'Gfg' : [6, 7, 3, 1], 'is' : [9, 1, 4], 'best' : [10, 7, 4]},
K = 'Gfg', i = 1
Output : 7
Explanation : 1st index of 'Gfg''s value is 7.
Input : test_dict = {'Gfg' : [6, 7, 3, 1], 'is' : [9, 1, 4], 'best' : [10, 7, 4]},
K = 'best', i = 0
Output : 10
Explanation : 0th index of 'best''s value is 10.
Method 1: Using + get()
This is one of the ways in which this task can be performed. In this, we extract the key's value using get() and then the value is extracted after checking for K being less than list length.
Python3
# Python3 code to demonstrate working of
# Extract ith element of K key's value
# Using get()
# initializing dictionary
test_dict = {'Gfg': [6, 7, 3, 1],
'is': [9, 1, 4],
'best': [10, 7, 4]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K
K = 'Gfg'
# initializing i
i = 2
# using get() to get the required value
temp = test_dict.get(K)
res = None
# checking for non empty dict and length constraints
if temp and len(temp) >= i:
res = temp[i]
# printing result
print("The extracted value : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 7, 3, 1], 'is': [9, 1, 4], 'best': [10, 7, 4]}
The extracted value : 3
Time complexity: O(1), as getting a value from a dictionary is an O(1) operation in Python, assuming the key exists.
Auxiliary space: O(1) as well, as the amount of memory used does not grow with the size of the dictionary.
Method 2: Using indexing
Python3
# Python3 code to demonstrate working of
# Extract ith element of K key's value
# initializing dictionary
test_dict = {'Gfg' : [6, 7, 3, 1],
'is' : [9, 1, 4],
'best' : [10, 7, 4]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K
K = 'Gfg'
# initializing i
i = 2
res=test_dict[K][i]
# printing result
print("The extracted value : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 7, 3, 1], 'is': [9, 1, 4], 'best': [10, 7, 4]}
The extracted value : 3
Time Complexity : O(N)
Auxiliary Space : O(1)
Method 3: Using Loops
- Initialize a variable result to None.
- Loop through the values of the K key in the dictionary using a for loop.
- For each value, check if the length of the value is greater than i.
- If the length is greater than i, set the result variable to the ith element of the value.
- Break out of the loop once the result variable has been set.
- Print the result.
Python3
# Python3 code to demonstrate working of
# Extract ith element of K key's value
# initializing dictionary
test_dict = {'Gfg' : [6, 7, 3, 1],
'is' : [9, 1, 4],
'best' : [10, 7, 4]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K
K = 'Gfg'
# initializing i
i = 2
# extract ith element using indexing
res = test_dict.get(K, [])[i]
# printing result
print("The extracted value : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 7, 3, 1], 'is': [9, 1, 4], 'best': [10, 7, 4]}
The extracted value : 3
Time complexity: O(n), where n is the length of the list value of the K key.
Auxiliary space: O(1), since we only use a constant amount of extra space to store the result variable.
Method 4: using list comprehension.
Step-by-step approach:
- Initialize the dictionary test_dict.
- Print the original dictionary.
- Initialize K and i.
- Use list comprehension to extract the ith element of the K key's value.
- Print the extracted value.
Below is the implementation of the above approach:
Python3
# initializing dictionary
test_dict = {'Gfg' : [6, 7, 3, 1],
'is' : [9, 1, 4],
'best' : [10, 7, 4]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K and i
K = 'Gfg'
i = 2
# extract ith element using list comprehension
res = [test_dict[key][i] for key in test_dict if key == K][0]
# printing result
print("The extracted value : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 7, 3, 1], 'is': [9, 1, 4], 'best': [10, 7, 4]}
The extracted value : 3
Time complexity: O(N), where N is the number of keys in the dictionary.
Auxiliary space: O(1), because only one variable is used to store the extracted value.
Method 5: Using the map() and filter() functions
STEPS :
- Start by initializing the dictionary test_dict.
Print the original dictionary using the print() function.
Initialize the variables K and i with the given values.
Use the map() function to extract the ith element of each value list in the dictionary. This is done by passing a lambda function to map() that accesses the ith element of the value list. Store the results in a new list extracted_values.
Use the filter() function to extract the value list for the given key K from the dictionary. This is done by passing a lambda function to filter() that checks if the current key is equal to K. Store the result in a new list filtered_values.
Extract the first element from filtered_values (which is the value list for the key K) and assign it to a variable value_list.
Extract the first element from extracted_values (which is the ith element of the value list for the key K) and assign it to a variable res.
Print the extracted value using the print() function.
Python3
# initializing dictionary
test_dict = {'Gfg' : [6, 7, 3, 1],
'is' : [9, 1, 4],
'best' : [10, 7, 4]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K and i
K = 'Gfg'
i = 2
# extract ith element using map() and filter()
extracted_values = list(map(lambda x: x[i], test_dict.values()))
filtered_values = list(filter(lambda x: x[0] == K, test_dict.items())) # filter items whose key equals K
value_list = filtered_values[0][1] # get the list of values associated with K
res = extracted_values[list(test_dict.keys()).index(K)] # find the index of K and use it to get the corresponding extracted value
# printing result
print("The extracted value : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 7, 3, 1], 'is': [9, 1, 4], 'best': [10, 7, 4]}
The extracted value : 3
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary, due to the creation of two new lists extracted_values and filtered_values.
Similar Reads
Python - Extract ith Key's Value of K's Maximum value dictionary Given Dictionary List, extract i'th keys value depending upon Kth key's maximum value. Input : test_list = [{"Gfg" : 3, "is" : 9, "best" : 10}, {"Gfg" : 8, "is" : 11, "best" : 19}, {"Gfg" : 9, "is" : 16, "best" : 1}], K = "best", i = "is" Output : 11 Explanation : best is max at 19, its correspondin
9 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
Python | Extract key-value of dictionary in variables Sometimes, while working with dictionaries, we can face a problem in which we may have just a singleton dictionary, i.e dictionary with just a single key-value pair, and require to get the pair in separate variables. This kind of problem can come in day-day programming. Let's discuss certain ways in
5 min read
Python - Extracting Key from Value Substring Sometimes, while working with Python dictionaries, we can have a problem in which we need to find the key from given value, querying substring from key's value. This kind of problem is common and have application in many domains including web development. Lets discuss certain ways in which this task
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