Python’s bracket notation is a powerful feature that allows you to access, slice, and manipulate sequences like lists, strings, and tuples. This comprehensive guide will help you understand how to use brackets effectively in Python, with clear examples and explanations.
Contents
Basic Indexing
Let’s start with the fundamentals of accessing individual elements:
# Basic positive indexing (starts at 0)
my_list = ['a', 'b', 'c', 'd', 'e']
print(my_list[0]) # Output: 'a' (first element)
print(my_list[2]) # Output: 'c' (third element)
# Negative indexing (starts at -1)
print(my_list[-1]) # Output: 'e' (last element)
print(my_list[-2]) # Output: 'd' (second-to-last element)
# Remember: Think of negative indices as "counting from the end"
# -1 is the last element, -2 is second-to-last, and so on
Basic Slicing
Slicing lets you extract a portion of a sequence. The syntax is [start:stop:step]
:
# Basic slice syntax: [start:stop]
# Note: The stop index is exclusive!
sequence = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Get first three elements
print(sequence[0:3]) # Output: [0, 1, 2]
# Get elements from index 4 to 7
print(sequence[4:8]) # Output: [4, 5, 6, 7]
# Omitting start starts from beginning
print(sequence[:5]) # Output: [0, 1, 2, 3, 4]
# Omitting end goes until the end
print(sequence[7:]) # Output: [7, 8, 9]
# Using negative indices in slices
print(sequence[-3:]) # Output: [7, 8, 9]
print(sequence[:-2]) # Output: [0, 1, 2, 3, 4, 5, 6, 7]
Advanced Slicing with Step
The step parameter allows you to take elements at regular intervals:
# Full slice syntax: [start:stop:step]
numbers = list(range(10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Take every second element
print(numbers[::2]) # Output: [0, 2, 4, 6, 8]
# Take every third element starting from index 1
print(numbers[1::3]) # Output: [1, 4, 7]
# Negative step reverses the sequence
print(numbers[::-1]) # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
# Take every second element counting backwards
print(numbers[::-2]) # Output: [9, 7, 5, 3, 1]
Working with Strings
String slicing works exactly the same way as list slicing:
text = "Python Programming"
# Basic character access
print(text[0]) # Output: 'P'
print(text[-1]) # Output: 'g'
# String slicing
print(text[0:6]) # Output: 'Python'
print(text[7:]) # Output: 'Programming'
# Reverse the string
print(text[::-1]) # Output: 'gnimmargorP nohtyP'
# Extract every second character
print(text[::2]) # Output: 'Pto rgamn'
Multi-dimensional Arrays and Nested Structures
When working with nested structures, you can use multiple brackets or combine them:
# 2D list (matrix)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Access single element (row 1, column 2)
print(matrix[1][2]) # Output: 6
# Get entire row
print(matrix[1]) # Output: [4, 5, 6]
# Get entire column (using list comprehension)
print([row[1] for row in matrix]) # Output: [2, 5, 8]
# Working with numpy arrays (if numpy is installed)
import numpy as np
np_matrix = np.array(matrix)
# Get column using single brackets
print(np_matrix[:, 1]) # Output: array([2, 5, 8])
Advanced Use Cases
Slice Assignment
You can assign values to slices to modify sequences:
# Modify a portion of a list
numbers = [0, 1, 2, 3, 4, 5]
numbers[2:4] = [20, 30]
print(numbers) # Output: [0, 1, 20, 30, 4, 5]
# Replace elements with a different number of elements
numbers[1:4] = [10]
print(numbers) # Output: [0, 10, 4, 5]
# Insert elements without removing any
numbers[2:2] = [2, 3]
print(numbers) # Output: [0, 10, 2, 3, 4, 5]
Deleting Elements
Using slices with del statement:
numbers = list(range(10))
# Delete a range of elements
del numbers[3:6]
print(numbers) # Output: [0, 1, 2, 6, 7, 8, 9]
# Delete every second element
del numbers[::2]
print(numbers) # Output: [1, 6, 8]
Common Patterns and Tricks
Here are some useful patterns that often come up in Python programming:
# Copy a list
original = [1, 2, 3]
copy = original[:] # Creates a shallow copy
# Get first n elements
first_three = sequence[:3]
# Get last n elements
last_three = sequence[-3:]
# Get all except first and last
middle = sequence[1:-1]
# Reverse a sequence
reversed_seq = sequence[::-1]
# Every nth element
every_third = sequence[::3]
Performance Considerations
When working with large sequences, keep these points in mind:
- Slicing creates a new object with copied elements
- For large sequences, consider using itertools.islice for memory efficiency
- When possible, use views (like memoryview) instead of copies
- Numpy arrays provide more efficient slicing for numerical data
# Memory-efficient slicing with itertools
from itertools import islice
large_sequence = range(1000000)
# Get every 100th number from first 500 numbers
result = list(islice(large_sequence, 0, 500, 100))
Common Mistakes to Avoid
- Confusing Stop Index:
# The stop index is exclusive!
sequence = [0, 1, 2, 3, 4]
print(sequence[1:3]) # Output: [1, 2] (not [1, 2, 3])
- Index Out of Range:
# This will raise IndexError
sequence = [1, 2, 3]
# print(sequence[5]) # IndexError!
# But slicing handles out-of-range gracefully
print(sequence[5:10]) # Output: [] (empty list)
- Modifying While Slicing:
# Be careful when modifying sequences while iterating
numbers = [1, 2, 3, 4, 5]
# Avoid this:
for i in range(len(numbers)):
if numbers[i] % 2 == 0:
del numbers[i] # This will cause problems!
# Better approach:
numbers = [x for x in numbers if x % 2 != 0]
Practice Exercises
To reinforce your understanding, try these exercises:
- Write a function that reverses a string without using the built-in reversed():
def reverse_string(s):
return s[::-1]
- Extract alternate characters from a string:
def alternate_chars(s):
return s[::2]
- Check if a string is palindrome:
def is_palindrome(s):
return s == s[::-1]
Remember that Python’s slice notation is one of its most powerful features. While it might seem complex at first, regular practice will make it second nature. The ability to manipulate sequences efficiently using slice notation is a key skill for Python programming.
Tags: Python, Programming, Data Structures, Slicing, Indexing