- Learning Objectives
- Prerequisites
- Introduction
- What is List Slicing?
- Why is List Slicing Required?
- Syntax
- Components of Slice
- Positive Indexing
- Negative Indexing
- Step Value
- Slice Assignment
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand list slicing.
- Use start, stop, and step values.
- Apply positive and negative indexing.
- Reverse lists using slicing.
- Modify lists using slice assignment.
- Apply slicing in real-world programs.
Before learning this chapter, you should know:
- Lists
- Indexing
- List Comprehension
- Operators
Lists often contain many elements.
Sometimes,
we need only a portion of a list instead of the entire list.
Examples include:
- First five students
- Last three records
- Every alternate value
- Reverse order
Instead of using loops,
Python provides List Slicing.
List Slicing is the process of extracting a portion of a list using index positions.
List slicing creates a new list containing selected elements from an existing list.
Without slicing,
extracting part of a list requires loops.
List slicing provides a shorter and more readable solution.
Example
numbers = [10,20,30,40,50]
print(numbers[1:4])Output
[20, 30, 40]
list[start:stop]list[start:stop:step]The index from where slicing begins.
Included in the result.
The index where slicing ends.
Not included in the result.
Determines how many positions to move between elements.
Default
1
List
A B C D E
0 1 2 3 4
Example
letters = ['A','B','C','D','E']
print(letters[1:4])Output
['B', 'C', 'D']
List
A B C D E
-5 -4 -3 -2 -1
Example
letters = ['A','B','C','D','E']
print(letters[-4:-1])Output
['B', 'C', 'D']
If the start index is omitted,
Python begins from index
0
Example
numbers = [10,20,30,40]
print(numbers[:3])Output
[10, 20, 30]
If the stop index is omitted,
Python continues until the last element.
Example
numbers = [10,20,30,40]
print(numbers[2:])Output
[30, 40]
Example
numbers = [10,20,30]
print(numbers[:])Output
[10,20,30]
This creates a shallow copy of the list.
Example
numbers = [1,2,3,4,5,6,7,8]
print(numbers[::2])Output
[1,3,5,7]
Using
[::-1]Example
numbers=[1,2,3,4]
print(numbers[::-1])Output
[4,3,2,1]
Slices can also replace existing elements.
Example
numbers=[10,20,30,40]
numbers[1:3]=[200,300]
print(numbers)Output
[10,200,300,40]
Original List
↓
Read Start Index
↓
Read Stop Index
↓
Read Step
↓
Create New List
↓
Copy Selected Elements
↓
Return New List
- Easy extraction.
- Cleaner code.
- Faster than manual looping for many slicing tasks.
- Supports reversing.
- Supports copying.
- Use meaningful index values.
- Use slicing instead of loops for simple extraction.
- Keep slice expressions readable.
- Use
[::-1]only when reversing is intended. - Understand that slicing creates a new list.
Assuming the stop index is included.
Example
numbers[1:4]Returns
1,2,3
Not
1,2,3,4
Using incorrect negative indices.
Using step value
0This raises
ValueErrorAssuming slicing modifies the original list.
Slicing creates a new list unless used with slice assignment.
numbers=[10,20,30,40,50]
print(numbers[1:4])[20,30,40]
numbers=[10,20,30,40,50]
print(numbers[:3])[10,20,30]
numbers=[10,20,30,40,50]
print(numbers[-3:])[30,40,50]
numbers=[1,2,3,4,5,6,7,8]
print(numbers[::2])[1,3,5,7]
numbers=[1,2,3,4,5]
print(numbers[::-1])[5,4,3,2,1]
numbers=[1,2,3,4,5,6,7,8]
print(numbers[::-2])[8,6,4,2]
numbers=[10,20,30]
copy=numbers[:]
print(copy)[10,20,30]
numbers=[10,20,30,40]
numbers[1:3]=[200,300]
print(numbers)[10,200,300,40]
numbers=[10,20,30,40,50]
del numbers[1:4]
print(numbers)[10,50]
text=list("PYTHON")
print(text[1:5])['Y','T','H','O']
List
↓
Start Index
↓
Stop Index
↓
Step Value
↓
Selected Elements
↓
New List
↓
Output
Original List
│
├──10
├──20
├──30
├──40
└──50
↓
Slice [1:4]
↓
New List
│
├──20
├──30
└──40
- Simple syntax.
- Easy extraction.
- Efficient copying.
- Supports reversing.
- Improves readability.
- Keep slice expressions simple.
- Use negative indexing carefully.
- Avoid unnecessary slicing.
- Use slice assignment only when modification is required.
- Assuming stop index is included.
- Using invalid step values.
- Confusing copying with referencing.
- Misusing negative indices.
List slicing extracts a portion of a list using index positions.
list[start:stop:step]No.
The stop index is excluded.
list[::-1]Yes.
copy = list[:]creates a shallow copy.
Python starts from the beginning of the list.
Python continues until the end of the list.
Slicing itself returns a new list.
However, slice assignment can modify the original list.
- Extract the first five elements.
- Extract the last four elements.
- Print alternate elements.
- Reverse a list.
- Replace elements using slice assignment.
- Delete elements using slicing.
- Slice Syntax →
list[start:stop:step] - Start → Included
- Stop → Excluded
- Step → Increment
[::-1]→ Reverse[:]→ Copy- Slice Assignment → Modify original list
In this chapter, you learned:
- List Slicing
- Slice Syntax
- Positive Indexing
- Negative Indexing
- Step Value
- Reverse Slicing
- Slice Assignment
- Practical Programs
- Advantages
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision