- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Get the Index of first element greater than K
The values of items in a python list are not necessarily in any sorted order. More over there may be situation when we are interested only in certain values greater than a specific value. In this article we will see how we can get the
Using Enumeration
Using enumeration we get both the index and value of the elements in the list. Then we apply the greater than condition to get only the first element where the condition is satisfied. The next function goes through each list element one by one.
Example
List = [21,10,24,40.5,11] print("Given list: " + str(List)) #Using next() + enumerate() result = next(k for k, value in enumerate(List) if value > 25)print("Index is: ",result)
Running the above code gives us the following result
Output
Given list: [21, 10, 24, 40.5, 11] Index is: 3
Using Filter and Lambda Functions
In the next example we take a lambda function to compare the given value with value at each index and then filter out those which satisfy the required condition. From the list of elements satisfying the required condition, we choose the first element at index 0 for our answer.
Example
List = [21,10,24,40.5,11] print("Given list: " + str(List)) #Using filter() + lambda result = list(filter(lambda k: k > 25, List))[0] print("Index is: ",List.index(result))
Running the above code gives us the following result
Output
Given list: [21, 10, 24, 40.5, 11] Index is: 3
Using map and lambda
In the next example we take a similar approach but use map instead of filter. The map function is used to loop through each of the elements. Whenever the condition becomes true that index is captured.
Example
List = [21,10,24,40.5,11] print("Given list: " + str(List)) result = list(map(lambda k: k > 25, List)).index(True) print("Index is: ",(result))
Running the above code gives us the following result
Output
Given list: [21, 10, 24, 40.5, 11] Index is: 3
- Related Articles
- Find smallest element greater than K in Python
- Python Indices of numbers greater than K
- Python – Average of digit greater than K
- Python – Remove characters greater than K
- Python - Consecutive Ranges of K greater than N
- Python – Filter Tuples Product greater than K
- Python - Number of values greater than K in list
- Count of subarrays whose maximum element is greater than k in C++
- Python – Remove Tuples with difference greater than K
- Python – Extract dictionaries with values sum greater than K
- Python – Find the frequency of numbers greater than each element in a list
- Python – Extract list with difference in extreme values greater than K
- Get first element of each sublist in Python
- Get first index values in tuple of strings in Python
- Python – Sort Matrix by Number of elements greater than its previous element
