Data Structures16 min read

Indexing and Slicing

Learn indexing, slicing, and step slicing for lists/tuples/strings with strong intuition, so you stop guessing and start slicing confidently.

David Miller
December 26, 2025
1.5k62

Indexing and slicing are core skills for working with sequences:

  • list
  • tuple
  • string

Indexing recap

data = ["a", "b", "c", "d"]
print(data[0])   # a
print(data[-1])  # d

Slicing format

data[start:end] includes start but excludes end.

data = ["a", "b", "c", "d", "e"]
print(data[1:4])  # ['b','c','d']

Slice with step

data[start:end:step]

nums = [0,1,2,3,4,5,6,7,8,9]
print(nums[0:10:2])  # [0,2,4,6,8]

Reverse a sequence

print(nums[::-1])

Common beginner mistakes

  • forgetting end is exclusive
  • mixing up negative indexes

Graph: slicing mental model

flowchart LR
  A[Sequence] --> B[start at index]
  B --> C[stop before end]
  C --> D[optional step]

Remember

  • end index is not included
  • step helps skip and reverse
  • slicing works across list/tuple/string
#Python#Beginner#Core Skills