Title: How do I count the last elements in a list after a certain element?
Tags: python;list;sum;counting
Question: I have a python list containing zeros and ones like this:
a = [1.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0]
I know how to count the ones and zeros in this, but what I can't manage to figure out is how to count the last zeros after the last 1.0 in that list. In this case the solution would be &quot;2&quot;.
I would like to have a simple code which I can use for this problem in order to put it in a loop.
I hope someone can help me with that. Thank you!

Here is the accepted answer: Try this:
a = [1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]
a[75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0-1].index(1)
You reverse the list, and take the index of the first 1.

Here is another answer: An alternative using functional programming:
from itertools import takewhile
from operator import eq
from functools import partial
equal_0 = partial(eq, 0)
a = [1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]
res = sum(1 for _ in takewhile(equal_0, reversed(a)))
print(res)
Output

Here is another answer: Another solution, if you want to use explicit for-loop:
a = [1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]
cnt = 0
for v in reversed(a):
if v:
break
cnt += 1
print(cnt)
Prints:

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:07/29/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><list><sum><counting>', 'Title': 'How do I count the last elements in a list after a certain element?', 'ViewCount': 36, 'id': 73170964, 'user_AboutMe': None, 'user_AccountId': 23958689.0, 'user_DisplayName': 'Kats', 'user_Reputation': 61.0, 'user_Views': 5.0, 'user_id': 17949141.0}}