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

MonkeeSage MonkeeSage at
Sun Oct 8 01:15:52 CEST 2006

On Oct 7, 12:37 pm, Fredrik Lundh <fred... at> wrote:
> for what?

  key in self.keys()

And d.get() looks like sugar for:

  if self.has_key(key):
    return self[key]
    return default_value

Why not have the same sugar for sequence types? E.g.,

  def has_index(self, index):
    return index < len(self)

  def get(self, index, default=None):
    if self.has_index(index):
      return self[index]
    return default

I understand not wanting to add bloat without use cases and / or  when
the programmer can easily implement the functionality themselves; but I
don't see why the sugar would be added for dict but not sequence types.
The fact of easy implementation by the programmer without the
convenience method is the same in both cases, and I would assume (but
don't claim to know) that one could find many use cases for, e.g.,
list.has_index() / . get().


More information about the Python-list mailing list