Python - Variable list slicing

In this article we will learn about variable list slicing. List slicing is a powerful feature of Python that allows you to extract specific portions of any list quickly. Python provides various techniques and methods for slicing based on specific criteria or patterns.

Consider this example ?

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
print("Original list:", numbers)
print("Sliced from index 8 to end:", numbers[8:])
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Sliced from index 8 to end: [9, 10, 33, 34, 56, 43, 67]

Let's explore different methods to perform list slicing effectively.

Basic Slicing Syntax

The basic slicing syntax is list[start:end:step], which extracts elements from start index to end index (exclusive) with an optional step value ?

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
sliced_numbers = numbers[2:15:2]

print("Original list:", numbers)
print("Sliced with step 2:", sliced_numbers)  
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Sliced with step 2: [3, 5, 7, 9, 33, 56, 67]

This example slices from index 2 to 15 (exclusive), taking every second element.

Using List Comprehension with Slicing

You can combine slicing with list comprehension to add conditions while extracting elements ?

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
even_slice = [num for num in numbers[2:9] if num % 2 == 0]

print("Original list:", numbers)
print("Even numbers from slice [2:9]:", even_slice)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67] 
Even numbers from slice [2:9]: [4, 6, 8]

This extracts even numbers from indices 2 to 9, filtering with the condition num % 2 == 0.

Slicing from Beginning

Omit the start index to slice from the beginning of the list ?

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
first_six = numbers[:6]

print("Original list:", numbers)
print("First 6 elements:", first_six)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
First 6 elements: [1, 2, 3, 4, 5, 6]

Using [:6] extracts the first 6 elements from index 0 to 5.

Slicing to the End

Omit the end index to slice from any starting point to the end of the list ?

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
from_index_8 = numbers[8:]

print("Original list:", numbers)
print("From index 8 to end:", from_index_8)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
From index 8 to end: [9, 10, 33, 34, 56, 43, 67]

Using [8:] extracts all elements starting from index 8 until the end.

Skipping Elements with Step

Use the step parameter to skip elements in a regular pattern ?

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
every_second = numbers[::2]

print("Original list:", numbers)
print("Every second element:", every_second)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67] 
Every second element: [1, 3, 5, 7, 9, 33, 56, 67]

Using [::2] extracts every second element from the entire list.

Negative Indexing

Use negative indices to count from the end of the list ?

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
negative_slice = numbers[-8:-2]

print("Original list:", numbers)
print("From -8 to -2:", negative_slice)  
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67] 
From -8 to -2: [8, 9, 10, 33, 34, 56]

This slices from the 8th element from the end to the 2nd element from the end (exclusive).

Comparison of Slicing Methods

Method Syntax Use Case
Basic slicing [start:end:step] General-purpose slicing
From beginning [:end] Get first N elements
To the end [start:] Get elements from index onwards
With step [::step] Skip elements regularly
Negative indexing [-start:-end] Count from the end

Conclusion

List slicing is an essential skill in Python programming. These different techniques basic slicing, list comprehension, negative indexing, and step values provide flexible ways to extract data from lists based on your specific requirements.

Updated on: 2026-03-27T14:54:00+05:30

766 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements