Python List

List slicing

List slicing is a powerful technique in Python that allows you to extract a sublist (a portion of the list) based on specific starting and ending positions. It uses a colon (:) syntax with optional start and stop indices within square brackets [].


1. Basic slicing

my_list = ["apple", "banana", "cherry", "orange", "mango"]
sublist = my_list[1:3]  # This extracts elements at index 1 (inclusive) to 3 (exclusive)
print(sublist)  # Output: ["banana", "cherry"]

In this example, the sublist captures elements from index 1 ("banana") to index 2 ("cherry") but excludes the element at index 3 ("orange").


2. Specifying start and stop indices

You can explicitly define the starting and ending positions for the slice:

my_list = ["apple", "banana", "cherry", "orange", "mango"]
first_two = my_list[0:2]  # Get elements from index 0 (inclusive) to 2 (exclusive)
last_three = my_list[2:]  # Get elements from index 2 (inclusive) to the end

print(first_two)  # Output: ["apple", "banana"]
print(last_three)  # Output: ["cherry", "orange", "mango"]

Here, first_two grabs elements from the beginning (index 0) up to but not including index 2. last_three fetches elements from index 2 (inclusive) to the end of the list.


3. Omitting start or stop indices

Leaving the start or stop index empty assumes default values:

  • An empty start index defaults to the beginning (index 0).
  • An empty stop index defaults to the end of the list.

my_list = ["apple", "banana", "cherry", "orange", "mango"]
all_but_last = my_list[:-1]  # Get all elements except the last one
first_three = my_list[:3]  # Get elements from the beginning up to index 3 (exclusive)

print(all_but_last)  # Output: ["apple", "banana", "cherry", "orange"]
print(first_three)  # Output: ["apple", "banana", "cherry"]


4. Negative indices

Similar to accessing elements, you can use negative indices for start and stop positions to count from the end of the list.

my_list = ["apple", "banana", "cherry", "orange", "mango"]
last_two = my_list[-2:]  # Get the last two elements
all_but_first = my_list[1:]  # Get all elements except the first one (similar to using an empty start index)

print(last_two)  # Output: ["orange", "mango"]
print(all_but_first)  # Output: ["banana", "cherry", "orange", "mango"]


It's time to take a quiz!

Test your knowledge and see what you've just learned.

What does the expression my_list[1:3] return?

["banana", "cherry"]
["apple", "banana", "cherry"]
["cherry", "orange"]
["orange", "mango"]
Check Answer

What does my_list[0:2] return?

["apple", "banana"]
["banana", "cherry"]
["cherry", "orange"]
["orange", "mango"]
Check Answer

What does my_list[:-1] return?

All elements including the last one.
All elements except the last one.
Only the last element.
An empty list.
Check Answer

What does my_list[-2:] return?

The last element.
The first two elements.
The last two elements.
All elements except the last one.
Check Answer

What does my_list[1:] return?

All elements from the start.
All elements including the first one.
All elements except the first one.
All elements except the last one.
Check Answer