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

MonkeeSage MonkeeSage at gmail.com
Sat Oct 7 13:26:22 EDT 2006



On Oct 7, 3:27 am, Gabriel Genellina <gagsl... at yahoo.com.ar> wrote:
> The meaning comes from the most common usage.

I wasn't suggesting that the "in" keyword have a different sematic for
sequence types. I was just saying that regarding the question whether
there is anything similar to "dict.has_key / k in dict" for lists, the
"in" keyword is not semantically analogous.

> Because it's not needed at all: valid sequence indexes are *exactly*
> range(len(seq)). This is the basic point of being a sequence: when
> indexes are not contiguous, in fact you have a mapping, not a sequence.

True. But valid dictionary keys are exactly d.keys(). The has_key
method is just sugar.

> Sometimes, maybe... But since you can write it efficientely in a few
> lines when needed, I don't see the need to put it into the core language.

Couldn't the same be said for dictionaries? I could write for
sequences:

if index < len(seq) and seq[index]: ...

But I could also write for dictionaries:

if key in dic.keys() and dic[key]: ...

I just wonder why the syntactic sugar was added for dicts, but not for
sequence types.

Regards,
Jordan




More information about the Python-list mailing list