Open In App

Python | Values till False element

Many a times we require to find the first occurring False 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 False element and returns the number of Truths before a False value i.e a zero value. 






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

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

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.



  Method #2 : Using filter() + lambda + 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 False value that is processed by the lambda functions and index function returns the first occurrence of this. 




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

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

Time complexity: O(n*n), where n is the length of the test_list. The filter() + lambda + index() takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required

Method #3: Using not operator and slicing




# Python3 code to demonstrate
# Values till False element
 
# initializing list
test_list = [1, 5, 0, 0, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
for i in range(len(test_list)):
    if(not test_list[i]):
        index = i
test_list = test_list[:index-1]
# printing result
print("The values till first False value : " + str(test_list))

Output
The original list is : [1, 5, 0, 0, 6]
The values till first False value : [1, 5]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using index, try and except methods.

Step-by-step approach:

Below is the implementation of the above approach:




test_list = [1, 5, 0, 0, 6]
try:
    index = test_list.index(False)
    test_list = test_list[:index]
except ValueError:
    pass
print("The values till first False value : " + str(test_list))

Output
The values till first False value : [1, 5]

Time complexity: O(n), where n is the length of the list. 
Auxiliary space: O(1), as we are not using any extra space to store the list values.


Article Tags :