In Python, a list is a very flexible data structure that can store multiple elements, which can be of different types (e.g., numbers, strings, or even other lists). To retrieve the elements we need from a list, we use two key tools: “indexing” and “slicing”.
1. List Indexing: Accessing Individual Elements¶
Each element in a list has a corresponding position number called the “index”. In Python, list indices start from 0—meaning the first element has an index of 0, the second has 1, and so on.
Positive Index Example:
fruits = ["apple", "banana", "cherry", "date"]
# Access the first element (index 0)
print(fruits[0]) # Output: apple
# Access the third element (index 2)
print(fruits[2]) # Output: cherry
Negative Index Example:
To access elements from the end of the list, we use negative indices. -1 refers to the last element, -2 to the second-to-last, and so on.
# Access the last element (index -1)
print(fruits[-1]) # Output: date
# Access the second-to-last element (index -2)
print(fruits[-2]) # Output: cherry
⚠️ Note: If an index is out of range (e.g., a positive index ≥ the list length, or a negative index < -list length), an IndexError will occur. For example:
# Error example: Index 4 exceeds the list length (length is 4, max index is 3)
print(fruits[4]) # Error: IndexError: list index out of range
2. List Slicing: Getting Sub-lists¶
Slicing is used to extract multiple elements from a list at once. The syntax is list[start:end:step], where:
- start: Starting position of the slice (inclusive).
- end: Ending position of the slice (exclusive).
- step: Step size (default is 1, meaning elements are taken every 1 position).
2.1 Basic Slicing (Left-Closed, Right-Open)¶
Slicing follows the “left-closed, right-open” principle: start is included, but end is excluded.
Examples:
numbers = [0, 1, 2, 3, 4, 5]
# From index 1 to 3 (inclusive of 1, exclusive of 3)
print(numbers[1:3]) # Output: [1, 2]
# From index 2 to the end (inclusive of 2)
print(numbers[2:]) # Output: [2, 3, 4, 5]
# From the start to index 3 (exclusive of 3)
print(numbers[:3]) # Output: [0, 1, 2]
2.2 Slicing with a Step Size¶
The step parameter controls the interval between elements. For example, step=2 means elements are taken every 2 positions.
Examples:
# Step 2, from start to 6 (exclusive)
print(numbers[::2]) # Output: [0, 2, 4]
# From index 1 to 5 (exclusive), step 2
print(numbers[1:5:2]) # Output: [1, 3]
2.3 Reverse Slicing (Negative Step Size)¶
When step is negative, slicing proceeds from right to left. In this case, start is typically greater than end (otherwise, an empty list is returned).
Examples:
# Reverse the entire list (step -1)
print(numbers[::-1]) # Output: [5, 4, 3, 2, 1, 0]
# From index 4 to 1 (exclusive), step -1 (right to left)
print(numbers[4:1:-1]) # Output: [4, 3, 2]
3. Common Pitfalls¶
- Slicing Does Not Error: Even if
startorendare out of bounds, Python returns an empty list (no error). For example:
numbers = [0, 1, 2]
print(numbers[10:20]) # Output: [] (start out of range)
print(numbers[:-5]) # Output: [] (end is negative and < -length)
- Slicing vs. Original List: Slicing returns a “copy” of the original list. Modifying the sliced result does not affect the original list (unless explicitly reassigned). For example:
sub = numbers[1:3]
sub[0] = 99 # Modify the sublist
print(numbers) # Original list remains unchanged, Output: [0, 1, 2]
4. Summary¶
List indexing and slicing are core tools for working with sequential data in Python:
- Indexing: Use list[i] to access a single element. Indices start at 0 or -1, and out-of-bounds indices cause errors.
- Slicing: Use list[start:end:step] to extract sublists. It follows the left-closed, right-open rule and can control direction (forward/backward) via step.
Practice scenarios like extracting the first N elements, reversing a list, or taking every other element to master these techniques!