break out of list comprehension?
<p>I am taking in an integer value, finding the factorial of that value and trying to count the number of trailing zeros if any are present. For example:</p>

<pre><code>def zeros(n):
    import math
    factorial = str(math.factorial(n))
    zeros_lst = [number if number == "0" (else) for number in factorial[::-1]]
    return len(zeros_lst)
</code></pre>

<p>The "else" in parenthesis is where the issue is occurring. I want to leave the loop if the as soon as it encounters a number that is not zero. I tried using break like you normally would, then looking up some examples but found nothing of similarity. </p>

<p>If someone knows how to break from a list comprehension or if is even possible that would be great. I am sure there are better ways to solve this problem, please post if you do. </p>

<p>Here is a function that will count the zeros, you just need to pass it your number. This saves the string operations you had before. It will terminate once there are no more trailing zeros.</p>

<pre><code>def count_zeros(n):
    n_zeros = 0;
    while True:
        if n%10 == 0:
            n = n/10
            n_zeros+=1
        else:
            return n_zeros

print(count_zeros(math.factorial(12)))
</code></pre>

python list-comprehension break