Last modified: Mar 25, 2026 By Alexander Williams

Python Array Slice Guide: Syntax & Examples

Python array slicing is a powerful feature. It lets you access parts of a sequence. You can use it on lists, strings, and tuples. This guide explains how it works.

Slicing is efficient. It creates a new object from the original. The original data stays unchanged. This is key for safe data manipulation.

Basic Slicing Syntax

The slice syntax uses colons. The basic form is sequence[start:stop]. The start index is inclusive. The stop index is exclusive.

This means the slice goes up to, but does not include, the stop index. It's a common point of confusion for beginners.


# Create a simple list
my_list = [10, 20, 30, 40, 50, 60, 70]

# Slice from index 2 to 5
slice_result = my_list[2:5]
print(slice_result)
    

[30, 40, 50]
    

The result contains elements at indices 2, 3, and 4. Index 5 (value 60) is not included. This is the exclusive stop rule.

Omitting Start and Stop

You can omit the start or stop index. Omitting start defaults to 0. Omitting stop defaults to the sequence's end. This is very useful.


my_list = [10, 20, 30, 40, 50]

# Omit start (starts from 0)
first_three = my_list[:3]
print("First three:", first_three)

# Omit stop (goes to the end)
from_two_on = my_list[2:]
print("From index 2:", from_two_on)

# Omit both (creates a full copy)
full_copy = my_list[:]
print("Full copy:", full_copy)
    

First three: [10, 20, 30]
From index 2: [30, 40, 50]
Full copy: [10, 20, 30, 40, 50]
    

Using my_list[:] is a common way to create a shallow copy. It's different from just assigning with =.

The Step Argument

The full slice syntax is sequence[start:stop:step]. The step argument controls the stride. It lets you skip elements.

A positive step moves forward. A negative step moves backward. This enables reversing sequences easily.


numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Get every second element
evens = numbers[::2]
print("Every second:", evens)

# Get elements from 2 to 8, step 3
custom = numbers[2:8:3]
print("2 to 8 step 3:", custom)

# Reverse the entire list
reversed_list = numbers[::-1]
print("Reversed:", reversed_list)
    

Every second: [0, 2, 4, 6, 8]
2 to 8 step 3: [2, 5]
Reversed: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    

The step argument adds great flexibility. It's perfect for sampling data or reversing order without a separate function.

Negative Indexing in Slices

Python supports negative indices. -1 refers to the last element. -2 refers to the second-to-last, and so on. This works perfectly in slices.

It's ideal for getting elements relative to the end. You don't need to know the exact Python array length.


data = ['a', 'b', 'c', 'd', 'e', 'f']

# Get the last three elements
last_three = data[-3:]
print("Last three:", last_three)

# Get all but the last two
all_but_last_two = data[:-2]
print("All but last two:", all_but_last_two)

# Complex slice with negative start and step
slice_neg = data[-2:2:-1]
print("Slice -2:2:-1:", slice_neg)
    

Last three: ['d', 'e', 'f']
All but last two: ['a', 'b', 'c', 'd']
Slice -2:2:-1: ['e', 'd']
    

Mixing negative indices with steps can be tricky. Practice with simple examples first. Understanding the index boundaries is crucial.

Slicing Strings and Tuples

Slicing isn't just for lists. It works on any sequence type. This includes strings and tuples. The behavior is consistent.


# Slicing a string
text = "Hello, World!"
part = text[7:12]
print("String slice:", part)

# Slicing a tuple
coordinates = (10, 20, 30, 40, 50)
tuple_slice = coordinates[1:4]
print("Tuple slice:", tuple_slice)
    

String slice: World
Tuple slice: (20, 30, 40)
    

The principle is the same. You get a new string or tuple. The original immutable object remains untouched.

Modifying Lists with Slices

You can use slices to modify lists. Assign a new sequence to a slice. This changes the list in place. It's a powerful way to edit.

This is different from using methods like Python array append. Slicing allows bulk replacement.


colors = ['red', 'green', 'blue', 'yellow', 'purple']
print("Original:", colors)

# Replace a slice
colors[1:4] = ['cyan', 'magenta']
print("After replacement:", colors)

# Insert elements (by replacing a zero-length slice)
colors[2:2] = ['black', 'white']
print("After insertion:", colors)

# Delete elements (assign empty list)
colors[-2:] = []
print("After deletion:", colors)
    

Original: ['red', 'green', 'blue', 'yellow', 'purple']
After replacement: ['red', 'cyan', 'magenta', 'purple']
After insertion: ['red', 'cyan', 'black', 'white', 'magenta', 'purple']
After deletion: ['red', 'cyan', 'black', 'white', 'magenta']
    

Slice assignment is very flexible. The new sequence can be a different length. This changes the overall Python array size.

Common Pitfalls and Best Practices

Slicing is intuitive but has edge cases. Be aware of them to avoid bugs.

Out-of-range indices are handled gracefully. Python doesn't throw an error. It just goes to the start or end of the sequence.


short_list = [1, 2, 3]
# Start index beyond length
print(short_list[10:])  # Returns empty list
# Stop index before start
print(short_list[3:1])  # Returns empty list
    

[]
[]
    

Slicing creates a shallow copy. For lists of objects, the new list contains references to the same objects. For a deep copy, you need extra steps.

Always test your slice logic. Write simple examples first. This builds confidence with the start:stop:step model.

Conclusion

Python array slicing is a core skill. It provides a concise syntax for accessing subsequences. Remember the start:stop:step format.

Use it to copy, reverse, sample, and modify data. It works on lists, strings, and tuples. Mastering slices makes your code more Pythonic and efficient.

Combine this knowledge with other array operations. For a deeper dive into sequence types, see our Python array implementation guide. Practice is key to becoming proficient.