Quark’s Outlines: Python Slice Objects
Overview, Historical Timeline, Problems & Solutions
An Overview of Python Slice Objects
What is a Python slice object?
When you want to take part of a list or string in Python, you use a slice. A Python slice object is what Python uses behind the scenes when you use slice notation like a[1:4] .
You can also create a slice object yourself using the built-in slice() function. A slice object has three parts: a start, a stop, and a step. These tell Python where to begin, where to end, and how far to jump between values.
Python lets you describe parts of a sequence using slice objects.
s = slice ( 1 , 5 , 2 ) print ( s . start , s . stop , s . step ) # prints: # 1 5 2
Enter fullscreen mode
Exit fullscreen mode
You can pass this object to any indexable type that accepts slices, such as lists, strings, or tuples.
Problem: How do you use Python slice objects with lists in Python?
You can apply a slice object directly to a list.…