Indexing and Slicing Lists
Overview
Learn how to access individual items in a list using indices and how to extract portions of lists using slicing. You'll understand zero-based indexing, negative indices, and how to use slice notation to get sublists efficiently.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- Indexing: Understand how to access individual items using their position.
- Zero-based indexing: Learn that Python lists start counting from 0.
- Negative indices: Discover how to access items from the end of a list.
- Slicing: Learn how to extract portions of lists using slice notation.
Why This Matters
Indexing and slicing are fundamental skills for working with lists. They let you access specific items or ranges of items efficiently. Whether you need the first item, the last item, or everything except the first two items, indexing and slicing give you precise control over which parts of a list you work with!
Step 1: Understanding Indexing
Indexing lets you access a specific item in a list by its position. Python uses zero-based indexing, which means the first item is at index 0, the second at index 1, and so on.
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # Output: apple (first item)
print(fruits[1]) # Output: banana (second item)
print(fruits[2]) # Output: orange (third item)
Zero-Based Indexing
Python starts counting from 0, not 1. The first item is at index 0, the second at index 1, and so on. This is different from how we count in everyday life, but it's standard in programming.
Square Bracket Notation
Use square brackets [] after the list name to access an item. Put the index number inside the brackets.
Index Out of Range
If you try to access an index that doesn't exist (like fruits[5] when the list only has 3 items), Python will raise an IndexError.
Key Concept: Think of indices as addresses. Each item in a list has an address (its index) that tells you exactly where it is. Index 0 is the first address, index 1 is the second address, and so on. This system makes it easy to access any item directly!
Mini Practice #1: Accessing Items by Index
Try It YourselfTry accessing different items in a list:
What happened? You accessed each item using its index. Notice that colors[0] gives you "red" (the first item), not "green". This is because Python uses zero-based indexing - counting starts from 0, not 1. The index tells Python exactly which position in the list you want!
Step 2: Negative Indices
You can also use negative indices to access items from the end of the list. Index -1 is the last item, -2 is the second-to-last, and so on.
fruits = ["apple", "banana", "orange"]
print(fruits[-1]) # Output: orange (last item)
print(fruits[-2]) # Output: banana (second-to-last)
print(fruits[-3]) # Output: apple (third-to-last)
How It Works
Negative indices count backwards from the end of the list. -1 always refers to the last item, -2 to the second-to-last, and so on. This is very useful when you don't know the length of the list but need to access items from the end!
Step 3: Slicing Lists
Slicing lets you extract a portion of a list. Use the syntax list[start:end] to get items from index start up to (but not including) index end.
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4]) # Output: [1, 2, 3] (indices 1, 2, 3)
print(numbers[:3]) # Output: [0, 1, 2] (from start to index 3)
print(numbers[2:]) # Output: [2, 3, 4, 5] (from index 2 to end)
print(numbers[:]) # Output: [0, 1, 2, 3, 4, 5] (entire list)
Start and End
The slice [start:end] includes items from index start up to but not including index end. So [1:4] gives you indices 1, 2, and 3.
Omitted Values
If you omit the start (like [:3]), it starts from the beginning. If you omit the end (like [2:]), it goes to the end. If you omit both (like [:]), you get the entire list.
Step Value
You can add a third number for the step: [start:end:step]. This lets you skip items or reverse the list.
Mini Practice #2: Slicing Lists
Try It YourselfTry slicing a list in different ways:
What happened? Slicing lets you extract portions of a list easily. letters[:3] gives you the first three items. letters[-2:] gives you the last two items (using negative indexing). letters[1:4] gives you items from index 1 to 3 (remember, the end index is not included). Slicing is a powerful way to work with parts of lists!
Step 4: Modifying Items by Index
You can change items in a list by assigning a new value to a specific index:
fruits = ["apple", "banana", "orange"]
fruits[1] = "grape"
print(fruits) # Output: ['apple', 'grape', 'orange']
Remember: Lists are mutable, which means you can change their contents. Simply assign a new value to an index to replace the item at that position. This modifies the original list!
End-of-Lesson Exercises
Exercise 1: Access Items
Create a list called numbers with values [10, 20, 30, 40, 50]. Print the first item (index 0), the last item (using negative index), and the middle item (index 2).
Use positive indices for the first and middle items, and negative index for the last item.
Exercise 2: Slice a List
Create a list called letters with values ["a", "b", "c", "d", "e", "f"]. Use slicing to get the first three items, the last two items, and items from index 2 to 4. Print each slice.
Use [:3] for first three, [-2:] for last two, and [2:5] for middle items.