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

MonkeeSage MonkeeSage at gmail.com
Sat Oct 7 21:06:47 EDT 2006


On Oct 7, 7:41 pm, Steven D'Aprano
<s... at REMOVE.THIS.cybersource.com.au> wrote:
> Are you just making a philosophical point? In which case I agree: *if* you
> make the analogy "a dictionary key is analogous to a sequence index",
> *then* the operation of "in" isn't semantically analogous between mappings
> and sequences. But why should it be?

It shouldn't, and I'm making a pragmatic (not merely philosophic) point
regarding the OP's question whether there is a "similar" list method to
dict.has_key.

> In both mappings and sequences, "in" tests for membership. But *what* it
> tests for membership is different. There's no shame there.

I know / agree.

> Originally, if you wanted to
> test for key membership with a dict, you had three choices:

I know.

On Oct 7, 7:59 pm, Steven D'Aprano
<s... at REMOVE.THIS.cybersource.com.au> wrote:
> Because they aren't needed often, and when they are, they are easy to
> implement?

More often and easier to implement than dict.has_key / get?

> It is hard to see where list.get(index, default) would be useful, since
> accessing an index out of range of a list is generally an error, unlike
> accessing a missing key.

Uh, no. KeyError.

> But I like symmetry, and for symmetry I wouldn't
> object to sequences gaining a get method too. Not that I care about it
> enough to put in a feature request or a PEP, but if somebody else did, I
> wouldn't object.

Me neither. :)

> get() is easy enough to implement. Feel free to turn it into a method
> of a collection class if you like.

I already did, see previous posts.

> Not every piece of functionality needs to be a built-in.

Agreed. but why implement a certain functionality in one place but
leave it out of another?

Regards,
Jordan




More information about the Python-list mailing list