Skip to content

Add notes about s[i] in Common Sequence Operations #134976

Closed
@Yzi-Li

Description

@Yzi-Li

Documentation

Operation Result Notes
s[i] ith item of s, origin 0 (3)
  1. If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0.

We can add a note: If the sequence is empty or i is outside the sequence range, IndexError will be raised.

Linked PRs

Activity

picnixz

picnixz commented on May 31, 2025

@picnixz
Member

I'm surprised that there is nothing about IndexError on this page

terryjreedy

terryjreedy commented on May 31, 2025

@terryjreedy
Member

I believe that while negative indexes are implemented for (most?) builtins, this is not necessarily the case for all sequences.

picnixz

picnixz commented on May 31, 2025

@picnixz
Member

Note (3) still says:

If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0.

So the common sequence operations assume that negative indices should be supported by arbitrary sequences.

added a commit that references this issue on Jun 8, 2025

gh-134976: document the exception type that can be raised by `s[i]` (#…

158e516
added a commit that references this issue on Jun 8, 2025

pythongh-134976: document the exception type that can be raised by `s…

added 2 commits that reference this issue on Jun 8, 2025

[3.14] gh-134976: document the exception type that can be raised by `…

483a462

[3.13] gh-134976: document the exception type that can be raised by `…

47a828e
picnixz

picnixz commented on Jun 8, 2025

@picnixz
Member

Thank you!

4 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Add notes about `s[i]` in `Common Sequence Operations` · Issue #134976 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant