Python | First occurrence of True number

Many times we require to find the first occurring non-zero number to begin the processing with. This has mostly use case in Machine Learning domain in which we require to process data excluding None or 0 values. Let’s discuss certain ways in which this can be performed.

Method #1 : Using next() + enumerate()
The next function can be used to iterate through the list and enumerate along with it checks for the list if the number is a non zero element and returns the number of 0s before a True value i.e a non zero value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# finding first True value 
# using next() and enumerate()
  
# initializing list 
test_list = [ 0, 0, 5, 6, 0]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# finding first True value
# using next() and enumerate()
res = next((i for i, j in enumerate(test_list) if j), None)
  
# printing result
print ("The values till first True value : " + str(res))

chevron_right


Output:

The original list is : [0, 0, 5, 6, 0]
The values till first True value : 2

 
Method #2 : Using filter() + lamda + index()
Using the combination of the above functions, one can easily perform this particular task. The filter function can be used to screen out the True value that is processed by the lambda functions and index function returns the first occurrence of this.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# finding first True value 
# using filter() + lamda + index()
  
# initializing list 
test_list = [ 0, 0, 5, 6, 0]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# finding first True value
# using filter() + lamda + index()
res = test_list.index(next(filter(lambda i: i != 0, test_list)))
  
# printing result
print ("The values till first True value : " + str(res))

chevron_right


Output:

The original list is : [0, 0, 5, 6, 0]
The values till first True value : 2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.