sobota 19. ledna 2013

Python: How find the first occurrence in list

If you only want the first thing that matches a condition (but you don't know what it is yet), it's fine to use a for loop (possibly using the else clause as well, which is not really well-known). You can also use
next(x for x in lst if ...)
which will return the first match or raise a StopIteration if none is found. Alternatively, you can use
next((x for x in lst if ...), [default value])
Source: http://stackoverflow.com/questions/9542738/python-find-in-list

Žádné komentáře:

Okomentovat