What Is List Slicing in Python and How Does It Work?
Learn how Python list slicing works, including negative indices, step values, reversing lists, and slice assignment, with clear code examples.
Expected Interview Answer
List slicing extracts a sub-list using the `list[start:stop:step]` syntax, returning a new list containing elements from index `start` up to but not including `stop`, advancing by `step`.
Any of the three parts can be omitted: omitting `start` defaults to the beginning, omitting `stop` defaults to the end, and omitting `step` defaults to 1. Negative indices count from the end, and a negative step reverses direction, so `list[::-1]` reverses the whole list. Slicing always returns a new list (a shallow copy) rather than a view, and slice assignment lets you replace, insert, or delete a contiguous chunk in place.
- Extracts sub-lists without manual loops
- Negative indices simplify accessing the end of a list
- list[::-1] reverses a sequence in one expression
- Slice assignment enables in-place insert/delete/replace
- Works identically on strings, tuples, and other sequences
AI Mentor Explanation
List slicing is like requesting the highlights reel for overs 10 through 15 of an innings — you specify the start over, the stop over, and get back a fresh clip containing just that stretch, not the whole match tape. A negative step is like watching that clip in reverse to study a batsman's dismissal shot by shot.
Step-by-Step Explanation
Step 1
Basic syntax
list[start:stop] returns elements from index start up to but excluding stop.
Step 2
Defaults
Omitted start defaults to 0, omitted stop defaults to len(list).
Step 3
Step parameter
list[start:stop:step] advances by step; a negative step walks backward.
Step 4
Negative indices
Negative indices count from the end, e.g. list[-1] is the last element.
Step 5
Slice assignment
list[1:3] = [...] replaces that range in place, and can insert or delete elements too.
What Interviewer Expects
- Explains the start:stop:step syntax accurately
- Knows stop is exclusive
- Can use negative indices and negative step correctly
- Knows slicing returns a new list (shallow copy)
- Can perform slice assignment to modify a list in place
Common Mistakes
- Assuming stop is inclusive
- Forgetting slicing returns a shallow copy, not a view
- Off-by-one errors with negative indices
- Not realizing list[::-1] reverses the whole list
Best Answer (HR Friendly)
“List slicing lets you pull out a portion of a list using a simple start:stop:step notation, like grabbing 'items 2 through 5' without writing a loop. It also supports reversing a list or skipping elements with a step value.”
Code Example
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums[2:5]) # [2, 3, 4]
print(nums[:3]) # [0, 1, 2]
print(nums[7:]) # [7, 8, 9]
print(nums[::2]) # [0, 2, 4, 6, 8]
print(nums[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
nums[1:3] = [100, 200, 300] # slice assignment can change length
print(nums) # [0, 100, 200, 300, 3, 4, 5, 6, 7, 8, 9]Follow-up Questions
- Does slicing a list create a shallow or deep copy?
- How would you reverse a list using slicing?
- How does slice assignment differ from indexing assignment?
- How does slicing work on strings and tuples?
- What does the slice() built-in object let you do?
MCQ Practice
1. What does nums[2:5] return?
Slicing is stop-exclusive, so nums[2:5] returns indices 2, 3, and 4.
2. How do you reverse a list using slicing?
A step of -1 with no start/stop walks the whole list backward, reversing it.
3. What type does list slicing return?
Slicing a list always produces a new list object (a shallow copy of the selected range).
Flash Cards
Is the stop index in a slice inclusive or exclusive? — Exclusive — list[a:b] excludes index b.
How do you reverse a list with slicing? — list[::-1]
What does slicing return? — A new list (shallow copy), never the original object.
How do negative indices work? — They count backward from the end, e.g. -1 is the last element.