Python – Similar other index element of K
Last Updated :
10 May, 2023
Given List of elements, other list and K, extract all the similar other list index elements if element is K in List.
Input : test_list = [6, 4, 6, 3, 6], arg_list = [7, 5, 4, 6, 3], K = 6
Output : [7, 4, 3]
Explanation : 7, 4 and 3 correspond to occurrences of 6 in first list.
Input : test_list = [2, 3], arg_list = [4, 6], K = 3
Output : [6]
Explanation : 6 corresponds to only occurrence of 3.
Method #1 : Using enumerate() + list comprehension
The combination of the above methods provides a way in which this task can be solved. In this, we check for like index from other list using enumerate() and create new list using list comprehension.
Python3
test_list = [ 5 , 7 , 3 , 2 , 3 , 8 , 6 ]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 5 , 8 , 3 , 7 , 9 , 3 ]
K = 3
res = [ele for idx, ele in enumerate (arg_list) if test_list[idx] = = K]
print ( "Extracted elements : " + str (res))
|
Output
The original list : [5, 7, 3, 2, 3, 8, 6]
Extracted elements : [8, 7]
Time complexity: O(n), where n is the length of the input lists test_list and arg_list, because the program iterates over each element of the lists once.
Auxiliary space: O(m), where m is the number of elements that satisfy the condition test_list[idx] == K because the program creates a list of those elements using list comprehension. However, this list is stored in memory temporarily and is not counted towards the space complexity of the program. Therefore, the actual auxiliary space used by the program is O(1).
Method #2 : Using list comprehension + zip()
The combination of the above functions can be used to solve this problem. In this, we perform combining each element with another among both lists using zip().
Python3
test_list = [ 5 , 7 , 3 , 2 , 3 , 8 , 6 ]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 5 , 8 , 3 , 7 , 9 , 3 ]
K = 3
res = [ele for ele, ele1 in zip (arg_list, test_list) if ele1 = = K]
print ( "Extracted elements : " + str (res))
|
Output
The original list : [5, 7, 3, 2, 3, 8, 6]
Extracted elements : [8, 7]
Time complexity: O(N), where n is the length of the input lists test_list and arg_list, because the program iterates over each element of the lists once.
Auxiliary space: O(M), where m is the number of elements in the list “test_list”.
Method #3: Using for loop
The code extracts the elements from arg_list that correspond to the indices where the value of K (3 in this case) is found in test_list. It then stores those extracted elements in a list called res.
Python3
test_list = [ 5 , 7 , 3 , 2 , 3 , 8 , 6 ]
arg_list = [ 4 , 5 , 8 , 3 , 7 , 9 , 3 ]
print ( "The original list : " + str (test_list))
K = 3
res = []
for i in range ( len (test_list)):
if test_list[i] = = K:
res.append(arg_list[i])
print ( "Extracted elements : " + str (res))
|
Output
The original list : [5, 7, 3, 2, 3, 8, 6]
Extracted elements : [8, 7]
The time complexity of this method is O(n), where n is the length of the input lists test_list and arg_list.
The space complexity is also O(n), as the size of the output list res can be up to n in the worst case.
Method #4:Using NumPy
Approach:
- Initialize the test_list with the given input list of integers.
- Initialize the arg_list with the given input list of integers.
- Initialize K with the given value.
- Convert both test_list and arg_list to NumPy arrays using np.array() function.
- Use np.where() function to get the indices of test_arr that match the value of K. This function returns a tuple with a single array containing the indices.
- Extract the elements from arg_arr corresponding to the indices obtained in step 5 using indexing.
- Print the extracted elements list.
Python3
import numpy as np
test_list = [ 5 , 7 , 3 , 2 , 3 , 8 , 6 ]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 5 , 8 , 3 , 7 , 9 , 3 ]
K = 3
test_arr = np.array(test_list)
arg_arr = np.array(arg_list)
idx = np.where(test_arr = = K)[ 0 ]
res = arg_arr[idx]
print ( "Extracted elements : " + str (res))
|
Output:
The original list : [5, 7, 3, 2, 3, 8, 6]
Extracted elements : [8 7]
Time complexity: O(N)
The time complexity of this approach is O(n), where n is the length of the input lists. The np.where() function scans the entire array to find the matching indices, which have a linear time complexity of O(n).
Auxiliary space:O(N)
The auxiliary space complexity of this approach is O(n), where n is the length of the input lists. This is because we are creating NumPy arrays of the input lists, which requires additional memory. The space required to store the result is also proportional to the number of matching indices, which can be up to the length of the input list.
Method #5: Using filter() + lambda function
Python3
test_list = [ 5 , 7 , 3 , 2 , 3 , 8 , 6 ]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 5 , 8 , 3 , 7 , 9 , 3 ]
K = 3
res = list ( filter ( lambda i: test_list[i[ 0 ]] = = K, enumerate (arg_list)))
res = [ele[ 1 ] for ele in res]
print ( "Extracted elements : " + str (res))
|
Output
The original list : [5, 7, 3, 2, 3, 8, 6]
Extracted elements : [8, 7]
Time complexity: O(n), where n is the length of the arg_list.
Auxiliary space O(k), where k is the number of indices in arg_list with elements equal to K.
Method #6: Using itertools.compress() + zip()
Step-by-step approach:
- Use zip() to combine the two lists test_list and arg_list into a list of tuples.
- Use itertools.compress() along with a generator expression to extract only the elements of arg_list where the corresponding element in test_list is equal to K.
- Convert the resulting iterator into a python list using the list() function.
Python3
from itertools import compress
test_list = [ 5 , 7 , 3 , 2 , 3 , 8 , 6 ]
print ( "The original list : " + str (test_list))
arg_list = [ 4 , 5 , 8 , 3 , 7 , 9 , 3 ]
K = 3
res = list (compress(arg_list, [i = = K for i in test_list]))
print ( "Extracted elements : " + str (res))
|
Output
The original list : [5, 7, 3, 2, 3, 8, 6]
Extracted elements : [8, 7]
Time complexity: O(n)
Auxiliary space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...