Open In App

Python | Get the Index of first element greater than K

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# to find index of first element just
# greater than K
# using enumerate() + next()
 
# initializing list
test_list = [0.4, 0.5, 11.2, 8.4, 10.4]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using enumerate() + next() to find index of
# first element just greater than 0.6
res = next(x for x, val in enumerate(test_list)
                                  if val > 0.6)
 
# printing result
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




# Python3 code to demonstrate
# to find index of first element just
# greater than K
# using filter() + lambda
 
# initializing list
test_list = [0.4, 0.5, 11.2, 8.4, 10.4]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using filter() + lambda
# to find index of first element just
# greater than 0.6
res = list(filter(lambda i: i > 0.6, test_list))[0]
 
# printing result
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




# Python3 code to demonstrate
# to find index of first element just
# greater than K
# using map() + index()
 
# initializing list
test_list = [0.4, 0.5, 11.2, 8.4, 10.4]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using map() + index()
# to find index of first element just
# greater than 0.6
res = list(map(lambda i: i> 0.6, test_list)).index(True)
 
# printing result
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):
    # creating a heap from the list
    heap = list(lst)
    heapq.heapify(heap)
 
    # using heappop() to find index of first element
    # just greater than k
    print(heap)
    for i, val in enumerate(heap):
        if val > k:
            res = i
            break
    else:
        res = None
 
    return res
 
# test the function
test_list = [0.4, 0.5, 11.2, 8.4, 10.4]
print(first_gt_index(test_list, 0.6))  # should print 2
 
 
#This code is contributed by Edula Vinay Kumar Reddy


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




# initializing list
test_list = [0.4, 0.5, 11.2, 8.4, 10.4]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using for loop to find index of
# first element just greater than 0.6
for i in range(len(test_list)):
    if test_list[i] > 0.6:
        res = i
        break
 
# printing result
print ("The index of element just greater than 0.6 : " + str(res))
#This code is contributed By Vinay Pinjala.


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:

  1. Initialize the list test_list with the given input.
  2. Print the original list.
  3. 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.
  4. Assign the resulting index to the variable index.
  5. Print the final result.

Python3




import bisect
 
# Initialize the list
test_list = [0.4, 0.5, 11.2, 8.4, 10.4]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# Use bisect_right to find the index of the first element greater than 0.6
index = bisect.bisect_right(test_list, 0.6)
 
# Print the result
print("The index of the first element greater than 0.6:", index)
#this code is contributed by Asif_Shaik


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
Previous
Next
Share your thoughts in the comments
Similar Reads