Python | Get the Index of first element greater than K
Last Updated :
09 Sep, 2024
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.
Python
# 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))
OutputThe 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.
Python
# 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)))
OutputThe 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.
Python
# 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))
OutputThe 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:
Python
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
Python
# 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.
OutputThe 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.
Python
import bisect
# Initialize the list
test_list = [0.4, 0.5, 8.4, 10.4, 11.2]
# 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
OutputThe 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.
Similar Reads
Python - Extract elements with Frequency greater than K
Given a List, extract all elements whose frequency is greater than K. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8], K = 3 Output : [4, 3] Explanation : Both elements occur 4 times. Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6], K = 2 Output : [4, 3, 6] Explanation : Occur 4, 3, and 3 time
7 min read
Python - Average of digit greater than K
Given elements list, extract elements whose average of digit is greater than K. Input : test_list = [633, 719, 8382, 119, 327], K = 5 Output : [719, 8382] Explanation : (7 + 1 + 9) / 3 = 5.6 and (8 + 3 + 8 + 2) / 4 = 5.2 , both of which are greater than 5, hence returned. Input : test_list = [633, 7
5 min read
Get the Last Element of List in Python
In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list: Input: list = [1, 3, 34, 12, 6]Output: 6 Explanation: Last element of the list l in the above example is 6. Let's explore various methods of doing it in Python: 1. Using
2 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 | Get first element with maximum value in list of tuples
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Get first and last elements of a list in Python
The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4]. Using indexingThis is the most straightforward way
3 min read
Python - Element Index in Range Tuples
Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t
6 min read
Python | Indices of numbers greater than K
Many times we might have problem in which we need to find indices rather than the actual numbers and more often, the result is conditioned. First approach coming to mind can be a simple index function and get indices greater than particular number, but this approach fails in case of duplicate number
4 min read
Python - Extract ith element of K key's value
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]
6 min read
Python - Extract Kth element of every Nth tuple in List
Given list of tuples, extract Kth column element of every Nth tuple. Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)], K = 2, N = 3 Output : [3, 8, 10] Explanation : From 0th, 3rd, and 6th tuple, 2nd elements are 3, 8, 10. Input :test_list
8 min read