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)
Similar Reads
Python - Elements with K lists similar index value
Sometimes, while working with data, we can have a problem in which we need to get elements which are similar in K lists in particular index. This can have application in many domains such as day-day and other domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using z
5 min read
Python | Extract similar index elements
Sometimes, while working with Python data, we can have a problem in which we require to extract the values across multiple lists which are having similar index values. This kind of problem can come in many domains. Let's discuss certain ways in which this problem can be solved. Method #1 : Using loo
6 min read
Python - Similar index elements frequency
Sometimes, while working with Python list, we can have a problem in which we need to check if one element has similar index occurrence in other list. This can have possible application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using sum() + zip() The
7 min read
Python - Group similar elements into Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are the same. This kind of problem can have applications in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [1, 3, 4, 4,
8 min read
Python - Index Ranks of Elements
Given a list of elements, our task is to get the index ranks of each element. [Tex]Index Rank of Number = (Sum of occurrence indices of number) / number[/Tex] Input : test_list = [3, 4, 6, 5, 3, 4, 9, 1, 2, 1, 8, 3, 2, 3, 9] Output : [(1, 16.0), (2, 10.0), (3, 9.333333333333334), (4, 1.5), (5, 0.6),
3 min read
Python - Group Records on Similar index elements
Sometimes, while working with Python records, we can have a problem in which we need to perform grouping of particular index of tuple, on basis of similarity of rest of indices. This type of problems can have possible applications in Web development domain. Let's discuss certain ways in which this t
6 min read
Python - Elements with same index
Given a List, get all elements that are at their index value. Input : test_list = [3, 1, 8, 5, 4, 10, 6, 9] Output : [1, 4, 6] Explanation : These elements are at same position as its number.Input : test_list = [3, 10, 8, 5, 14, 10, 16, 9] Output : [] Explanation : No number at its index. Method #1:
5 min read
Python | Test list element similarity
Given a list, your task is to determine the list is K percent same i.e has element that is populated more than K % times. Given below are few methods to solve the task. Method #1 Using collections.Counter C/C++ Code # Python3 code to demonstrate # to check whether the list # K percent same or not fr
4 min read
Python | Get Kth element till N
Sometimes, we may come across a utility in which we require to get the first N sublist elements that too only a particular index. This can have an application in queuing to get only the Kth N personâs name. Letâs discuss certain ways in which this can be done. Method #1 : Using list comprehension an
3 min read
Python - Check Similar elements in Matrix rows
Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List. Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : True Explanation : All rows have same elements.Input : test_list = [[1, 1,
8 min read