Python Front and rear range deletion in a list?

Sometimes we need to remove elements from both the beginning and end of a list simultaneously. Python provides several approaches to delete elements from both the front and rear of a list efficiently.

Using List Slicing

This approach creates a new list by slicing elements from both ends. The original list remains unchanged ?

days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

# Given list
print("Given list:", days)

# Number of elements to delete from front and rear
v = 2

# Create new list excluding v elements from each end
new_list = days[v:-v]
print("New list:", new_list)
Given list: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
New list: ['Tue', 'Wed', 'Thu']

Using del Statement

This approach modifies the original list in-place using the del statement. We must delete from the rear first to maintain correct indices ?

days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

# Given list
print("Given list:", days)

# Number of elements to delete from front and rear
v = 2

# Delete from rear first, then front
del days[-v:], days[:v]
print("New list:", days)
Given list: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
New list: ['Tue', 'Wed', 'Thu']

Using pop() Method

This approach removes elements one by one using the pop() method. We remove from the end first to avoid index shifting ?

days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

# Given list
print("Given list:", days)

# Number of elements to delete from front and rear
v = 2

# Remove from rear first
for i in range(v):
    days.pop()

# Remove from front
for i in range(v):
    days.pop(0)

print("New list:", days)
Given list: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
New list: ['Tue', 'Wed', 'Thu']

Comparison

Method Modifies Original? Performance Best For
List Slicing No Fast When you need the original list
del Statement Yes Fast In-place modification
pop() Method Yes Slower When removing one by one

Conclusion

Use list slicing for creating a new list without modifying the original. Use del statement for efficient in-place modification. The pop() method is useful when you need the removed elements for further processing.

Updated on: 2026-03-15T18:37:54+05:30

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements