A timeline report extracts recent entries and a reversed view.
What You Need to Understand
List slicing with [start:stop:step] creates a new list using a half-open interval: start is included and stop is excluded. A negative step traverses backward.
When to Use It
A timeline report extracts recent entries and a reversed view.
Step-by-Step Python Example
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
print(days[1:4])
print(days[::2])
print(days[::-1])
How the Example Works
The first slice selects Tuesday through Thursday; the second skips every other entry; the third reverses the sequence.
Example Output
['Tue', 'Wed', 'Thu']
['Mon', 'Wed', 'Fri']
['Fri', 'Thu', 'Wed', 'Tue', 'Mon']
[4, 5, 6]
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
numbers = [1, 2, 3, 4, 5, 6]
print(numbers[-3:])
Important Behavior and Edge Cases
A zero step raises ValueError. Unlike single-element indexing, slicing outside boundaries does not normally raise IndexError.
Try It Yourself
Select the final three elements from a list with at least five entries.
Common Beginner Mistakes
- A zero step raises
ValueError. Unlike single-element indexing, slicing outside boundaries does not normally raiseIndexError. - Assuming every sequence operation modifies the original data or that a tuple makes nested mutable objects immutable.
Key Takeaways
- List slicing with
[start:stop:step]creates a new list using a half-open interval:startis included andstopis excluded. - The first slice selects Tuesday through Thursday; the second skips every other entry; the third reverses the sequence.
- A zero step raises
ValueError.