Python | Get the Index of first element greater than K
Python list operations are always desired to have shorthands as they are used in many places in development. Hence having knowledge of them always remains quite useful. Let’s deals with finding one such utility of having index of first element greater than K by one-liner. There are various ways in which this can be achieved.
Method #1 : Using next() + enumerate() Using next() returns the iterator to the element that has been using the enumerate(). We simply put the condition for enumerate and next() picks appropriate element index.
Python3
test_list = [ 0.4 , 0.5 , 11.2 , 8.4 , 10.4 ]
print ("The original list is : " + str (test_list))
res = next (x for x, val in enumerate (test_list)
if val > 0.6 )
print ("The index of element just greater than 0.6 : "
+ str (res))
|
Output:
The original list is : [0.4, 0.5, 11.2, 8.4, 10.4]
The index of element just greater than 0.6 : 2
Time Complexity: O(n), where n is the length of the input list. This is because we’re using next() + enumerate() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.
Method #2 : Using filter() + lambda Using filter along with lambda can also help us to achieve this particular task, subscript index value 0 is used to specify that first element greater than value has to be taken.
Python3
test_list = [ 0.4 , 0.5 , 11.2 , 8.4 , 10.4 ]
print ("The original list is : " + str (test_list))
res = list ( filter ( lambda i: i > 0.6 , test_list))[ 0 ]
print ("The index of element just greater than 0.6 : "
+ str (test_list.index(res)))
|
Output:
The original list is : [0.4, 0.5, 11.2, 8.4, 10.4]
The index of element just greater than 0.6 : 2
Method #3 : Using map() + index() map() along with the index() can also return the desired element index and has the similar internal working as method 1 as discussed above.
Python3
test_list = [ 0.4 , 0.5 , 11.2 , 8.4 , 10.4 ]
print ("The original list is : " + str (test_list))
res = list ( map ( lambda i: i> 0.6 , test_list)).index( True )
print ("The index of element just greater than 0.6 : "
+ str (res))
|
Output:
The original list is : [0.4, 0.5, 11.2, 8.4, 10.4]
The index of element just greater than 0.6 : 2
Method # : Using heap
To find the index of the first element greater than a given value using a heap, you can first create a heap from the input list, and then use the heappop() function to repeatedly pop the smallest element from the heap until you find an element that is greater than the given value.
Here is an example of how this could be implemented:
Python3
import heapq
def first_gt_index(lst, k):
heap = list (lst)
heapq.heapify(heap)
print (heap)
for i, val in enumerate (heap):
if val > k:
res = i
break
else :
res = None
return res
test_list = [ 0.4 , 0.5 , 11.2 , 8.4 , 10.4 ]
print (first_gt_index(test_list, 0.6 ))
|
Output
[0.4, 0.5, 11.2, 8.4, 10.4]
2
Time complexity: O(n * log(n)), as the heapify() function has a time complexity of O(n * log(n)), and the heappop() function has a time complexity of O(log(n)).
Auxiliary Space: O(n), as the heapify() function creates a new list to store the heap, which has a size equal to the input list.
Method #5 : Using for loop
Python3
test_list = [ 0.4 , 0.5 , 11.2 , 8.4 , 10.4 ]
print ( "The original list is : " + str (test_list))
for i in range ( len (test_list)):
if test_list[i] > 0.6 :
res = i
break
print ( "The index of element just greater than 0.6 : " + str (res))
|
Output
The original list is : [0.4, 0.5, 11.2, 8.4, 10.4]
The index of element just greater than 0.6 : 2
Time Complexity: O(n)
Space Complexity:O(1)
Method#6:using “bisect_right()”method from the bisect module
Step-by-step algorithm:
- Initialize the list test_list with the given input.
- Print the original list.
- Use the bisect_right function from the bisect module to find the index of the first element in test_list that is greater than the value 0.6.
- Assign the resulting index to the variable index.
- Print the final result.
Python3
import bisect
test_list = [ 0.4 , 0.5 , 11.2 , 8.4 , 10.4 ]
print ( "The original list is : " + str (test_list))
index = bisect.bisect_right(test_list, 0.6 )
print ( "The index of the first element greater than 0.6:" , index)
|
Output
The original list is : [0.4, 0.5, 11.2, 8.4, 10.4]
The index of the first element greater than 0.6: 2
Time complexity:
The time complexity of the algorithm is O(log n), where n is the length of the input list test_list. The bisect_right function uses binary search to find the insertion point for the value 0.6 in the sorted list test_list. The time complexity of binary search is logarithmic with respect to the length of the list.
Auxiliary space:
The auxiliary space complexity of the algorithm is O(1), constant. This is because the algorithm only creates a single variable index to store the result, which is independent of the size of the input list. The bisect_right function itself does not use any additional memory that depends on the size of the input list.
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...