Slicing
๐ What is Slicing? Slicing extracts a portion of a list or string using the syntax [start:end:step]. It returns a new list or string - the original is unchanged. Slicing is one of Python's most expressive features and is used constantly in data processing.

Appy Saysโฆ
Slicing is one of Python's most elegant features. Grab the first 10 items of a list, every other element, or reverse a string โ all in one expression. Once you understand slices, you'll use them in almost every program you write.
What are Slices?
A slice extracts a portion of a sequence (list, string, or tuple) using the [start:stop:step] syntax. Python returns a new sequence โ the original is unchanged.
- โข
items[2:5]โ elements at index 2, 3, 4 (stop is exclusive) - โข
items[:3]โ first three elements (start defaults to 0) - โข
items[3:]โ from index 3 to the end - โข
items[::2]โ every other element (step of 2) - โข
items[::-1]โ reverse the entire sequence - โข
items[-3:]โ last three elements (negative indices count from the end)
Think of it like a playlist clip
Imagine your Spotify playlist has 100 songs. A slice is like saying 'play songs 5 to 15' or 'play the last 3 songs' โ you're not deleting anything from the playlist, just picking a section to work with.
How It Works
- โข1.
seq[start:stop:step]โ all three parts are optional - โข2.
startdefaults to 0,stopdefaults to end,stepdefaults to 1 - โข3. Indices can be negative:
-1is the last element,-2is second-to-last - โข4.
stopis always exclusive โ[0:3]gives indices 0, 1, 2 - โข5. A negative
stepreverses direction:[::-1]reverses the sequence - โข6. Out-of-range indices are silently clipped โ no IndexError with slices
Real-World Examples
- โขPaginate results:
results[page*10 : (page+1)*10]โ 10 results per page - โขGet file extension:
filename[-4:]โ'.png' - โขReverse a string to check if it's a palindrome:
word[::-1] == word - โขShow preview of a long message:
message[:100] + 'โฆ' - โขProcess every other frame in a video:
frames[::2]
Key Facts
- โขSlices work on strings, lists, tuples and any custom sequence type
- โข
list[:]creates a shallow copy of the entire list โ useful for avoiding mutation bugs - โขNumPy arrays (used in data science and AI) heavily rely on slice syntax
- โขPython internally uses a
sliceobject โ you can create one withslice(start, stop, step)
Watch Out!
Slices return a shallow copy โ inner objects (like nested lists) are still shared with the original. If you slice a list of lists and modify an inner list, the change shows in both the slice and the original. Use copy.deepcopy() if you need full independence.
Remember
seq[start:stop:step] โ stop is exclusive, negatives count from the end, [::-1] reverses. Slicing never raises an IndexError.
What You Learned
- โขSlices extract portions of sequences:
seq[start:stop:step] - โขNegative indices count from the end;
[::-1]reverses any sequence - โขUnlocks: pagination, text previews, data sampling, palindrome checks, and clean list manipulation
Key Facts
- โSlices work on strings, lists, tuples and any custom sequence type
- โ
list[:]creates a shallow copy of the entire list โ useful for avoiding mutation bugs - โNumPy arrays (used in data science and AI) heavily rely on slice syntax
- โPython internally uses a
sliceobject โ you can create one withslice(start, stop, step)
Real-World Examples
Remember
seq[start:stop:step] โ stop is exclusive, negatives count from the end, [::-1] reverses. Slicing never raises an IndexError.
Quick Quiz
s[1:4] includes index 4?