how to break a for loop?

Steven D'Aprano steve at REMOVEMEcyber.com.au
Tue Feb 21 02:59:40 EST 2006


Petr Jakes wrote:

> zero_list=[0,0,0,0,0,1,2,3,4]
> for x in range(len(zero_list)):
>     if zero_list[x]!=0: break
> nonzero_list=zero_list[x:]
> print nonzero_list
> 
> but some more "pythonic" solutions will be posted from other users I
> guess :)


That looks perfectly Pythonic to me.

You know, just because Python has for loops and 
multi-line blocks of code doesn't mean we shouldn't use 
them *wink*


-- 
Steven.




More information about the Python-list mailing list