How to find an item/items in a list?
Alex Martelli
aleax at aleax.it
Tue May 6 04:55:19 EDT 2003
<posted & mailed>
Bjorn Pettersen wrote:
...
> def after(lst, item):
> try:
> # throws ValueError when not found,
> # TypeError if lst is a string and item is not
> index = lst.index(item)
> # throws IndexError when out-of-range
*blink* -- can you please give an example...???
> return lst[index+1:]
I thought slicing was notably soft in this regards. E.g.:
>>> lst = list('ciao')
>>> lst[lst.index('o')+1:]
[]
>>>
What IndexError? What am I missing?
> except ValueError, IndexError:
> # let TypeErrors through (too surprising).
> return []
I _think_ you only need worry about ValueError (accepting
your reasonable decision to propagate TypeError).
Alex
More information about the Python-list
mailing list