[Solved] how to find first index of any number except 0 in python list? [closed]
[ad_1] You could use enumerate to iterate over the indices and values. Then use next to stop upon finding the first non-zero value. If StopIteration was thrown, the list contained no non-zero values, so do whatever error handling you’d like there. def first_non_zero(values): try: return next(idx for idx, value in enumerate(values) if value != 0) … Read more