Ch 9 · Lists — Collections of Data · Python
Topic 9 of 13 in Python — Complete Tutorial (Beginner) — 3 lessons.
What is a List?
A list is an ordered collection of values stored together under one name. Before lists, storing five students' marks would need five separate variables. A list solves this: store all five together as marks = [88, 92, 75, 60, 95], and process them all with a single loop. Lists can hold numbers, strings, booleans, even other lists — and they can grow and shrink as your program runs.
Common List Methods
Lists come with many built-in methods for adding, removing, and inspecting items. append adds to the end, insert adds at a position, remove deletes by value, and pop removes and returns by index. You can check if something is in a list with the in keyword, count items, and sort or reverse them. Most of these methods change the list in place, so the original is modified.
Looping Through Lists
The most common thing you do with a list is process every item — print each one, add them up, check each against a condition. The cleanest way is a for loop directly over the list: for item in my_list: gives you each element in turn. When you also need each item's position, use enumerate(), which hands you both the index and the value together — far neater than managing a counter by hand.
All topics in Python Beginner
- Introduction to Programming & Python
- Your First Python Program
- Variables — Your Data Containers
- Data Types — Kinds of Values
- Operators & Expressions
- Conditional Statements
- Loops & Iterations
- Functions
- Lists — Collections of Data
- Dictionaries & Classes
- Strings — Working with Text
- Error Handling
- Mini-Projects & Practice