Python tutorials > Data Structures > Lists > What is list indexing/slicing?
What is list indexing/slicing?
Basic Indexing
[]
to access elements. Python uses zero-based indexing, so the first element is at index 0. Negative indices count from the end of the list, with -1 representing the last element. Attempting to access an index outside the list's bounds will raise an IndexError
.
my_list = ['apple', 'banana', 'cherry', 'date']
# Accessing the element at index 0 (the first element)
first_element = my_list[0]
print(f"The first element is: {first_element}")
# Accessing the element at index 2 (the third element)
third_element = my_list[2]
print(f"The third element is: {third_element}")
# Accessing the last element using negative indexing
last_element = my_list[-1]
print(f"The last element is: {last_element}")
Basic Slicing
:
operator within square brackets. The syntax is [start:end]
, where start
is the index of the first element to include (inclusive), and end
is the index of the element to exclude (exclusive). If start
is omitted, it defaults to 0 (the beginning of the list). If end
is omitted, it defaults to the length of the list (the end). A slice creates a new list; it doesn't modify the original.
my_list = ['apple', 'banana', 'cherry', 'date', 'fig']
# Slicing from index 1 (inclusive) to index 3 (exclusive)
slice_1 = my_list[1:3]
print(f"Slice from index 1 to 3: {slice_1}")
# Slicing from the beginning to index 4 (exclusive)
slice_2 = my_list[:4]
print(f"Slice from beginning to index 4: {slice_2}")
# Slicing from index 2 (inclusive) to the end
slice_3 = my_list[2:]
print(f"Slice from index 2 to end: {slice_3}")
# Creating a copy of the entire list
slice_4 = my_list[:]
print(f"Copy of the list: {slice_4}")
Slicing with a Step
step
value: [start:end:step]
. The step
determines the increment between elements in the slice. A negative step value allows you to create slices in reverse order. For example, [::-1]
reverses the list.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Slicing with a step of 2 (every other element)
slice_1 = my_list[::2]
print(f"Slice with step 2: {slice_1}")
# Slicing from index 1 to 7 with a step of 3
slice_2 = my_list[1:7:3]
print(f"Slice from index 1 to 7 with step 3: {slice_2}")
# Reversing a list using a negative step
reversed_list = my_list[::-1]
print(f"Reversed list: {reversed_list}")
Concepts Behind the Snippet
Real-Life Use Case
Best Practices
Interview Tip
When to Use Them
Memory Footprint
Alternatives
Pros of Indexing and Slicing
Cons of Indexing and Slicing
IndexError
exceptions.
FAQ
-
What happens if I try to access an index that's out of range?
You'll get anIndexError
. For example, if you have a list with 5 elements and try to access index 5 (the 6th element, remember zero-based indexing!), Python will raise this error. -
Does slicing modify the original list?
No, slicing creates a new list. The original list remains unchanged. -
How can I reverse a list using slicing?
You can reverse a list using the slice[::-1]
. This creates a new list with the elements in reverse order. -
Is it possible to modify a slice of a list?
Yes, you can assign new values to a slice of a list. For example:my_list[1:3] = ['new_item1', 'new_item2']
will replace the elements at indices 1 and 2 with the new values. The number of elements being replaced doesn't have to match the length of the slice; for example, you could replace two elements with a single one or with multiple ones.