access

list[i] / list[i:j:k]

Subscript syntax to read a single element (by index) or a new list (by slice). Slicing always returns a new shallow copy; out-of-range slice bounds are silently clamped while single-index access raises IndexError.

list[index]   |   list[start:stop:step]

Parameters

Parameter Purpose
index Single int; negative counts from the end. Out of range raises IndexError.
start:stop Half-open range [start, stop); missing bounds default to 0 and len(list).
step Stride; negative reverses (e.g. lst[::-1] is a reversed copy).

Examples

>>> lst = [10, 20, 30, 40]
>>> lst[1]
20
>>> lst[-1]
40

Negative indices count from the right.

>>> lst = [1, 2, 3, 4, 5]
>>> lst[1:4]
[2, 3, 4]

Slice returns a new list.

>>> lst = [1, 2, 3]
>>> lst[::-1]
[3, 2, 1]

Negative step reverses; a common idiom.

>>> lst = [1, 2, 3]
>>> lst[9]
IndexError: list index out of range
>>> lst[9:99]
[]

Single-index raises; slice bounds silently clamp.

Gotcha

Slicing produces a SHALLOW copy — nested objects are shared. lst[9] raises IndexError but lst[9:99] returns [] silently.

Related methods

← All Python list methods · Dict methods · list vs tuple vs set