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

hanumizzle hanumizzle at
Sat Oct 7 04:47:51 CEST 2006

On 6 Oct 2006 16:57:23 -0700, erikcw <erikwickstrom at> wrote:
> I ended up using len(sys.argv) > 1 for this particular problem.  But I
> think slicing is closer to the tool I was looking for.
> I found a.has_key(k) or "k in a" for dictionaries - but haven't found
> anything similar for lists.  Does it exist?

As Gabriel Genellina observed, lists also may use the 'in' syntax.
Actually, any object that supports the method __contains__ can do
this, which means you may furnish your own in user objects.

What hasn't been addressed is efficiency. This is often a secondary
concern in a VHLL such as Python, but you may wish to know that using
the 'in' test on a large list repeatedly will slow down a program. You
may wish to use a set object instead, according to the application.

> I guess my example from php would technically be a dictionary in python
> and not a list, it would nice to be able to quickly tell if a list key
> existed or not.

Yes, PHP arrays are associative (dicts). But lists (in the Pythonic
sense, aka 'vectors') do not have keys as such, only indices.

-- Theerasak

More information about the Python-list mailing list