Skip to content

A list and tuple accept a negative index, slicing doesn't get index out of range error and for slicing, the left index can be greater than the right index e.g. [10:-10] #137867

@hyperkai

Description

@hyperkai

Bug description:

A list and tuple have some problems as shown below:

*Problems:

  1. A negative index is possible to use so it should be impossible to use. *I've never used it in my life.
  2. Slicing doesn't get index out of range error while indexing gets it so slicing should also get index out of range error.
  3. For slicing, the left index can be greater than the right index, e.g. [10:-10] so they shouldn't be like that.

<List indexing>:

v = ['A', 'B', 'C', 'D', 'E']

print(v[2], v[-3])
# C C

print(v[-10], v[10])
# IndexError: list index out of range

<Tuple indexing>:

v = ('A', 'B', 'C', 'D', 'E')

print(v[2], v[-3])
# C C

print(v[-10], v[10])
# IndexError: tuple index out of range

<List slicing>:

v = ['A', 'B', 'C', 'D', 'E']

print(v[2], v[-3])
# C C

print(v[-10:10]) # No error
# ['A', 'B', 'C', 'D', 'E']

print(v[10:-10]) # No error
# []

<Tuple slicing>:

v = ('A', 'B', 'C', 'D', 'E')

print(v[2], v[-3])
# C C

print(v[-10:10]) # No error
# ('A', 'B', 'C', 'D', 'E')

print(v[10:-10]) # No error
# []

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions