Open In App

Python – Extract ith element of K key’s value

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output

The 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))


Output

The 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

  1. Initialize a variable result to None.
  2. Loop through the values of the K key in the dictionary using a for loop.
  3. For each value, check if the length of the value is greater than i.
  4. If the length is greater than i, set the result variable to the ith element of the value.
  5. Break out of the loop once the result variable has been set.
  6. 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))


Output

The 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:

  1. Initialize the dictionary test_dict.
  2. Print the original dictionary.
  3. Initialize K and i.
  4. Use list comprehension to extract the ith element of the K key’s value.
  5. 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))


Output

The 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))


Output

The 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads