ZetCode

Python slice Function

Last modified April 11, 2025

This comprehensive guide explores Python's slice function, which creates slice objects for sequence manipulation. We'll cover basic usage, advanced techniques, and practical examples of sequence slicing.

Basic Definitions

The slice function returns a slice object representing a range of indices. It's used to extract portions of sequences like strings, lists, and tuples. The function accepts up to three parameters: start, stop, and step.

Key characteristics: creates reusable slice objects, supports negative indices, and handles omitted parameters. Slice objects are used with the square bracket notation for sequence access.

Basic Sequence Slicing

Here's simple usage with different sequence types showing how slice can extract portions of strings, lists, and tuples.

basic_slice.py
# With strings
text = "Hello, World!"
s = slice(7, 12)
print(text[s])  # 'World'

# With lists
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
s = slice(2, 8, 2)
print(numbers[s])  # [2, 4, 6]

# With tuples
colors = ('red', 'green', 'blue', 'yellow')
s = slice(1, 3)
print(colors[s])  # ('green', 'blue')

This example shows slice with different sequence types. The slice object is created once and reused, making code cleaner and more efficient.

The step parameter (third argument) allows skipping elements. Negative indices count from the end of the sequence.

Negative Indices and Omitted Parameters

Slice objects support negative indices and handle omitted parameters gracefully. This example demonstrates these features.

negative_slice.py
data = [10, 20, 30, 40, 50, 60, 70, 80, 90]

# Negative indices
s1 = slice(-5, -1)
print(data[s1])  # [50, 60, 70, 80]

# Omitted start
s2 = slice(None, 4)
print(data[s2])  # [10, 20, 30, 40]

# Omitted stop
s3 = slice(6, None)
print(data[s3])  # [70, 80, 90]

# Only step
s4 = slice(None, None, 3)
print(data[s4])  # [10, 40, 70]

Negative indices count from the end of the sequence. Omitted parameters (None) default to the sequence boundaries. This makes slice objects very flexible.

The step parameter can be used alone to select every nth element from the entire sequence.

Reusing Slice Objects

Slice objects can be stored and reused with different sequences, making them powerful tools for consistent data extraction patterns.

reuse_slice.py
# Create a slice object for middle three elements
middle_three = slice(1, 4)

# Reuse with different sequences
names = ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve']
print(names[middle_three])  # ['Bob', 'Charlie', 'Dave']

temperatures = (32.5, 34.1, 29.8, 27.3, 25.9)
print(temperatures[middle_three])  # (34.1, 29.8, 27.3)

hex_values = '0123456789ABCDEF'
print(hex_values[middle_three])  # '123'

This demonstrates how a single slice object can be applied to multiple sequences. The same extraction pattern works consistently across different types.

This technique is particularly useful when you need to apply the same extraction logic to many sequences in your program.

Slice Object Attributes

Slice objects have three readable attributes: start, stop, and step. These can be inspected or modified for dynamic slicing behavior.

slice_attrs.py
s = slice(2, 10, 2)

print(s.start)  # 2
print(s.stop)   # 10
print(s.step)   # 2

# Modify slice dynamically
data = list(range(20))
s = slice(None, None, None)

for step in range(1, 4):
    s = slice(s.start, s.stop, step)
    print(f"Step {step}: {data[s]}")

The attributes provide access to the slice parameters. They can be used to create new slices or inspect existing ones programmatically.

Dynamic modification of slice objects enables flexible sequence processing patterns that adapt to runtime conditions.

Advanced Slicing Techniques

Slice objects can be combined with other Python features for powerful sequence manipulation. This example shows advanced usage.

advanced_slicing.py
# Slice assignment
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
s = slice(2, 6)
data[s] = ['a', 'b', 'c', 'd']
print(data)  # [0, 1, 'a', 'b', 'c', 'd', 6, 7, 8, 9]

# Multidimensional slicing
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
row_slice = slice(0, 2)
col_slice = slice(1, 3)
print([row[col_slice] for row in matrix[row_slice]])  # [[2, 3], [5, 6]]

# Slice in function arguments
def process_slice(sequence, slc):
    return sequence[slc]

s = slice(1, None, 2)
print(process_slice('abcdefgh', s))  # 'bdfh'

Slice assignment modifies portions of mutable sequences. Multidimensional slicing extracts sub-matrices. Passing slice objects to functions makes them more flexible.

These techniques demonstrate the full power of Python's slicing capabilities when combined with other language features.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.