Module 2 Lesson 2: Lists
Master Python's most versatile data structure: the List. Learn indexing, slicing, and manipulation techniques.
Module 2 Lesson 2: Lists
The List is the workhorse of Python. It is an ordered, changeable collection of items. In this lesson, we will learn how to build, access, and modify lists to manage large amounts of data.
Lesson Overview
In this lesson, we will cover:
- Creating Lists: Putting items in brackets.
- Indexing: Accessing specific items using their "address."
- Slicing: Taking a "chunk" out of a list.
- List Methods: Adding, removing, and sorting items.
- List Length: Using
len()to count items.
1. Creating a List
In Python, we use square brackets [] to create a list. Elements are separated by commas.
fruits = ["apple", "banana", "cherry"]
prime_numbers = [2, 3, 5, 7, 11]
mixed_list = ["Alex", 25, True, 1.85]
Note: Lists can hold any data type, and even a mix of types!
2. Indexing (Accessing Items)
Every item in a list has an Index (a position number). Remember: Python starts counting at 0.
colors = ["red", "green", "blue"]
print(colors[0]) # Output: red
print(colors[1]) # Output: green
print(colors[2]) # Output: blue
Negative Indexing: You can use negative numbers to count from the end!
-1is the last item.-2is the second-to-last item.
print(colors[-1]) # Output: blue
Visualizing List Indexing
graph LR
subgraph "List: ['red', 'green', 'blue']"
direction LR
A["Index 0<br/>-3<br/>'red'"]
B["Index 1<br/>-2<br/>'green'"]
C["Index 2<br/>-1<br/>'blue'"]
end
3. Slicing (Taking a Chunk)
You can specify a range of indexes to get a sub-list. The syntax is [start:stop]. The stop index is not included.
nums = [10, 20, 30, 40, 50, 60]
# Get items from index 1 to 3
middle = nums[1:4] # Result: [20, 30, 40]
# Get everything from the beginning to index 2
start = nums[:3] # Result: [10, 20, 30]
# Get everything from index 3 to the end
end = nums[3:] # Result: [40, 50, 60]
4. Modifying a List (Mutable)
Unlike strings, you can change a list after it's been created.
pets = ["cat", "dog", "bird"]
# Change the second item
pets[1] = "hamster"
print(pets) # Output: ["cat", "hamster", "bird"]
5. Common List Methods
Python has built-in "tools" (methods) to help us work with lists.
| Method | Description | Example |
|---|---|---|
.append(x) | Add x to the end | list.append("new") |
.insert(i, x) | Add x at position i | list.insert(0, "first") |
.remove(x) | Remove the first item named x | list.remove("apple") |
.pop() | Remove and return the last item | last = list.pop() |
.sort() | Sort list in alphabetical/numerical order | list.sort() |
6. List Length
How many items are in your list? Use the len() function.
tasks = ["code", "eat", "sleep"]
print(len(tasks)) # Output: 3
Practice Exercise: The Supermarket List
Create a file named supermarket.py.
- Create a list named
cartwith 3 items:"Milk","Eggs","Bread". - Add
"Apples"to the end of the list using.append(). - Add
"Soda"to the beginning of the list using.insert(). - Remove
"Bread"from the list. - Print the total number of items in the cart using
len(). - Print the final list, sorted alphabetically.
Quick Knowledge Check
- What is the index of the first item in a Python list?
- How do you access the very last item of a list without knowing its length?
- What is the difference between
.append()and.insert()? - If
x = [1, 2, 3, 4], what is the result ofx[1:3]?
Key Takeaways
- Lists are ordered and changeable (mutable).
- Indexing starts at 0.
- Slicing
[start:stop]creates a new list from a range. - Methods like
.append(),.remove(), and.sort()allow for easy manipulation.
What’s Next?
Lists are great, but sometimes we want data that can't be changed. In Lesson 3, we’ll explore Tuples and see why being unchangeable is actually a superpower!