Concepts / List Indexing and Slicing

List Indexing and Slicing

For the generated list shown in the diagram, index 0 identifies red, index 1 identifies blue, index 2 identifies green, and index 3 identifies yellow. Indexing refers to positions, so changing the list can change which item belongs to a later index.

  • Programming
Interactive lab

Try it: Indexing and Slicing

How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.

How it works

  1. Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
  2. An index outside the string raises IndexError.
  3. A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
  4. A negative step walks backwards; a step of 0 is a ValueError.

Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).

Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.

Educational simulation

Loading the simulation…

One List, Many Positions

A Python list keeps items in an ordered sequence. You create the sequence with square brackets and commas, and each item occupies a position called an index. The first index is 0, not 1. Once you understand that position system, you can select one item, select a range of items, add new items, remove items, and rearrange the list.

0red1blue2green3yellow
Which item does each zero-based index point to in an ordered list?
Key point

For the generated list shown in the diagram, index 0 identifies red, index 1 identifies blue, index 2 identifies green, and index 3 identifies yellow. Indexing refers to positions, so changing the list can change which item belongs to a later index.

Selecting Items by Index

To access an individual item, identify the list and then identify its index position. Because indexing starts at 0, the item at index 0 is the first item, the item at index 1 is the second item, and so on. Indexing does not describe the item's value; it describes where the item appears in the ordered list.

Worked example

Finding one item

Consider the ordered list ["tea", "coffee", "water"]. Which item is at index 1?

  1. 1
    Start at zero

    The first item, tea, is at index 0.

  2. 2
    Move to the next position

    The second item, coffee, is at index 1.

  3. 3
    Identify the result

    The item at index 1 is coffee.

Result

coffee

Selecting a Range

Slicing selects a range of items from an ordered list. A slice is described by a start index and a stop index. The selected range begins at the start position and continues through the positions before the stop position. This lets you work with several neighboring items without selecting each item separately.

0red1blue2green3yellowselected range
Which range of items is selected when a start and stop index define a slice?
Worked example

Reading a slice

Consider the ordered list ["red", "blue", "green", "yellow"]. What items are selected by a slice beginning at index 1 and stopping at index 3?

  1. 1
    Locate the start

    Index 1 identifies blue, so blue is included.

  2. 2
    Continue before the stop

    Index 2 identifies green, so green is included.

  3. 3
    Stop at the boundary

    Index 3 identifies yellow, but the stop position is not included.

Result

["blue", "green"]

Changing List Contents

Lists are mutable, which means list operations modify the original list directly. The append method adds an item to the list. The del statement removes an item by its index. After an item is added or removed, the ordered contents and the index positions can change.

[red, blue]indices 0, 1[red, blue, green]indices 0, 1, 2[red, green]indices 0, 1append greendel index 1
How does the list's contents and index positions change after an item is added or removed?
Worked example

Tracking a mutation

Start with ["red", "blue"]. Add green, then remove the item at index 1.

  1. 1
    Initial state

    The list contains red at index 0 and blue at index 1.

  2. 2
    Append

    Appending green adds it to the end, producing ["red", "blue", "green"].

  3. 3
    Delete

    Deleting index 1 removes blue. The remaining list is ["red", "green"].

Result

The original list now contains red at index 0 and green at index 1.

Reordering with Sort

The sort method arranges the items in a list alphabetically. Sorting changes the original list directly because a list is mutable. As the order changes, the item found at each index can change too.

Worked example

Following a sort

Start with ["pear", "apple", "banana"] and sort the list alphabetically.

  1. 1
    Inspect the initial order

    The items begin in the order pear, apple, banana.

  2. 2
    Arrange alphabetically

    Alphabetical order places apple first, banana second, and pear third.

  3. 3
    Read the new positions

    After sorting, apple is at index 0, banana is at index 1, and pear is at index 2.

Result

["apple", "banana", "pear"]

OperationPurposeEffect on the original list
IndexingAccess one item by positionDoes not describe a change
SlicingSelect a range of itemsSelects a range from the ordered list
appendAdd an itemChanges the list directly
delRemove an item by indexChanges the list directly
sortArrange items alphabeticallyChanges the list directly

List operations have different purposes, but append, del, and sort modify the original mutable list.

Mistakes to Avoid

  • Common mistake

    Using 1 as the first index

    What it looks like

    Treating the first item as if it were at index 1

    Why it fails

    Python list indexes start at 0.

    Do this instead

    Label the first item with index 0, the second with index 1, and continue from there.

  • Common mistake

    Including the stop position in a slice

    What it looks like

    Expecting a slice from start index 1 to stop index 3 to include the item at index 3

    Why it fails

    The stop position marks where the selected range ends.

    Do this instead

    Include the start position and the positions before the stop position.

  • Common mistake

    Assuming an index remains attached to an item forever

    What it looks like

    Using an old index after deleting an earlier item

    Why it fails

    Removing an item can shift later items to new positions.

    Do this instead

    Recheck the current list and its indexes after append, del, or sort.

  • Common mistake

    Forgetting that mutation affects the original list

    What it looks like

    Expecting append, del, or sort to leave the original list unchanged

    Why it fails

    Lists are mutable, and these operations modify the original list directly.

    Do this instead

    Treat the list as changed immediately after one of these operations.

Practice the Position System

MEDIUM

Consider the list ["oak", "pine", "maple", "birch"]. Identify the item at index 2, identify the items in the range beginning at index 1 and stopping at index 3, append "cedar", delete the item at index 0, and describe the new index of maple.

Hints
  • Count the first item as index 0.
  • A slice includes its start position and stops before its stop position.
  • After deleting index 0, the remaining items move toward the beginning of the list.
Best practice

When working with a list, first write down the current order and its zero-based indexes. After append, del, or sort, update that position map before accessing an item by index. This makes changes to the original mutable list easier to follow.

Key Takeaways

  • A list is an ordered, mutable collection created with square brackets and commas.
  • List indexes start at 0, so the first item is at index 0.
  • Slicing selects a range beginning at a start index and ending before a stop index.
  • append adds an item, del removes an item by index, and sort arranges items alphabetically.
  • Because lists are mutable, append, del, and sort modify the original list directly and can change index positions.

More concepts starting with L