Can't get around "IndexError: list index out of range"

Fredrik Lundh fredrik at pythonware.com
Sun Oct 8 14:44:02 EDT 2006


MonkeeSage wrote:

> With list.has_index() / get(), the following (pretty common I think)
> idiom:
> 
> try:
>   data = some_unknown_seq[2]
> except IndexError:
>   data = None
> if data: ...

umm.  you could at least write:

     try:
         data = some_unknown_seq[2]
     except IndexError:
         pass
     else:
         ... deal with data ...

but "let's hypergeneralize and treat sequences and mappings as the same 
thing" proposals are nothing new; a trip to the archives might be help-
ful.

</F>




More information about the Python-list mailing list