Open In App
Related Articles

Python | Values till False element

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# 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

# 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

# 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:

  • Initialize the list to be operated upon.
  • Iterate through the list and check if the element is False.
    • If False is found, save the index and break out of the loop.
    • If False is not found, the index remains as the last index.
      • Slice the list up to the index and save it in the original list.
  • Print the original list with the values till the first False value.

Below is the implementation of the above approach:

Python3

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.



Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads