Python | Multiplication till Null value

The prefix array is quite famous in the programming world. This article would discuss a variation of this scheme. This deals with the cumulative list product till a False value, and again starts cumulation of product from occurrence of True value. Let’s discuss certain way in which this can be performed.

Method #1 : Using Naive Method
In the naive method, we just construct the new list comprising of the the product of prev. value of list until False and restarts the procedure once a True value is encountered.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Multiplication till Null value
# using naive method 
  
# initializing list of lists
test_list = [1, 3, 4, False, 4, 5, False, 7, 8]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# Multiplication till Null value
# using naive method
for i in range(1, len(test_list)):
    if test_list[i]: 
        test_list[i] *= test_list[i - 1]
    else:
        test_list[i] = 1
  
# printing result
print ("The computed modified new list : " + str(test_list))

chevron_right


Output :

The original list is : [1, 3, 4, False, 4, 5, False, 7, 8]
The computed modified new list : [1, 3, 12, 1, 4, 20, 1, 7, 56]
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.