0
0
Pythonprogramming~5 mins

List indexing and slicing in Python

Choose your learning style9 modes available
Introduction

List indexing and slicing help you get parts or single items from a list easily. It is like picking fruits from a basket by their position.

When you want to get the first or last item from a list.
When you want to get a group of items from the middle of a list.
When you want to reverse a list or get every other item.
When you want to copy a list or part of it.
When you want to check or change specific items in a list.
Syntax
Python
my_list = [10, 20, 30, 40, 50]

# Indexing
single_item = my_list[index]

# Slicing
sub_list = my_list[start:stop:step]

Indexing starts at 0 for the first item.

Slicing uses start (inclusive), stop (exclusive), and step (optional) to get parts of the list.

Examples
Index 0 gets the first item. Negative index -1 gets the last item.
Python
my_list = [10, 20, 30, 40, 50]
print(my_list[0])  # First item
print(my_list[-1]) # Last item
Slicing examples: from middle, from start, and from middle to end.
Python
my_list = [10, 20, 30, 40, 50]
print(my_list[1:4])  # Items from index 1 to 3
print(my_list[:3])   # First three items
print(my_list[2:])   # From index 2 to end
Step in slicing lets you skip items or reverse the list.
Python
my_list = [10, 20, 30, 40, 50]
print(my_list[::2])  # Every other item
print(my_list[::-1]) # Reverse the list
Edge cases: empty list slicing returns empty list, indexing single item works normally.
Python
empty_list = []
print(empty_list[0:1])  # Slicing empty list returns []

single_item_list = [100]
print(single_item_list[0])  # Indexing single item
print(single_item_list[0:1]) # Slicing single item
Sample Program

This program shows how to use indexing and slicing on different lists: normal, single item, and empty. It prints the results clearly.

Python
def print_list_info(my_list):
    print(f"Original list: {my_list}")
    if my_list:
        print(f"First item (index 0): {my_list[0]}")
        print(f"Last item (index -1): {my_list[-1]}")
    else:
        print("List is empty, no items to show.")
    print(f"Slice from index 1 to 3: {my_list[1:4]}")
    print(f"Slice first 3 items: {my_list[:3]}")
    print(f"Slice from index 2 to end: {my_list[2:]}")
    print(f"Every other item: {my_list[::2]}")
    print(f"Reversed list: {my_list[::-1]}")

# Test with a normal list
numbers = [10, 20, 30, 40, 50]
print_list_info(numbers)

print("\n")

# Test with a single item list
single_item_list = [100]
print_list_info(single_item_list)

print("\n")

# Test with an empty list
empty_list = []
print_list_info(empty_list)
OutputSuccess
Important Notes

Indexing and slicing are very fast operations with time complexity O(k), where k is the size of the slice.

Slicing creates a new list, so it uses extra space proportional to the slice size.

Common mistake: forgetting that the stop index in slicing is not included.

Use indexing to get single items, slicing to get parts or copies of lists.

Summary

Indexing gets one item by position, starting at 0.

Slicing gets a part of the list using start, stop, and step.

Negative indexes count from the end, and step can reverse or skip items.