Lists and For Loops
Imagine you go grocery shopping. Your shopping list might contain:
- Milk
- Bread
- Eggs
- Apples
- Rice
You could create five separate variables:
item1 = "Milk"item2 = "Bread"item3 = "Eggs"item4 = "Apples"item5 = "Rice"But this quickly becomes inconvenient. What if you have 100 items?
Python gives us a better solution: Lists
What Is a List?
A Python list allows you to store multiple values together.
For example:
shopping = ["Milk", "Bread", "Eggs", "Apples"]Think of a list like a row of labeled compartments:
shopping
↓
┌────────┬────────┬────────┬─────────┐
│ Milk │ Bread │ Eggs │ Apples │
└────────┴────────┴────────┴─────────┘
Instead of managing four separate variables, we have one list.
Creating a List
Python lists use square brackets:
numbers = [10, 20, 30, 40]Strings can also be stored:
fruits = ["Apple", "Banana", "Mango"]A list can contain different kinds of values too:
student = ["Aarav", 21, True]Although Python allows this, in real programs it is often clearer for a list to contain related kinds of information.
Lists and Arrays
You may already have heard the word array.
A Python list is often used for many of the tasks beginners associate with arrays. However, a Python list is not exactly the same thing as a traditional fixed-type array in languages such as Java or C.
For now, the useful mental model is: A list is an ordered collection of values.
Indexes
Every item in a Python list has a position called an index.
Python starts counting from zero. For example:
fruits = ["Apple", "Banana", "Mango"]The positions are:
Index: 0 1 2
↓ ↓ ↓
Apple Banana Mango
So:
fruits[0] means the first item.
fruits[1] means the second item.
fruits[2] means the third item.
Why Does Python Start at Zero?
This is one of those things that feels strange initially.
Imagine seats numbered from zero: 0 1 2 3 4
The first position is zero. Programming languages commonly use zero-based indexing because of how positions and offsets are represented internally. You do not need to understand the memory-level reason yet.
Just remember: The first item in a Python list has index 0.
Accessing List Items
Example:
fruits = ["Apple", "Banana", "Mango"]print(fruits[0])Output:
Apple
You can access the second item:
print(fruits[1])Output:
Banana
["Banana", "Cherry", "Date", "Elderberry"]
Why Negative Slicing Works: In Python, negative index -1 always points to the last element, -2 to the second to last, and [::-1] reverses any string or list in an instant without needing a loop!
The Problem With Repeating Code
Suppose we want to print every fruit. We could write:
print(fruits[0])print(fruits[1])print(fruits[2])This works. But imagine a list containing 10,000 items. You would not want to write 10,000 print statements.
This is where loops become extremely useful.
What Is a Loop?
A loop allows a program to repeat an operation.
Imagine a teacher checking homework from 30 students. The teacher follows the same basic process:
- Take one student's homework
- Check it
- Move to the next student
- Check it
- Move to the next student ...
A programming loop can perform the same type of repeated operation automatically.
The Python for Loop
Python gives us a very readable way to loop through a list:
fruits = ["Apple", "Banana", "Mango"]for fruit in fruits: print(fruit)Output:
Apple
Banana
Mango
This syntax is incredibly important:
for fruit in fruits: print(fruit)Read it like English: "For each fruit in fruits, print the fruit."
What Is item in for item in list?
The variable name can be anything.
For example:
for fruit in fruits: print(fruit)Here: fruit is the variable representing the current item.
And: fruits is the list we are going through.
You could instead write:
for item in fruits: print(item)This works too. Or:
for x in fruits: print(x)This also works. But for fruit in fruits: is easier to understand because the name describes the value.
How the Loop Works
Suppose:
fruits = ["Apple", "Banana", "Mango"]Python conceptually does something like:
First:fruit → Apple
print Apple
Second:fruit → Banana
print Banana
Third:fruit → Mango
print Mango
No more items → Stop
A diagram makes this easier to visualize.
graph TD
A[Start with list] --> B[Get next item]
B --> C[Store current item in fruit]
C --> D[Execute print(fruit)]
D --> E{More items?}
E -- Yes --> B
E -- No --> F[Loop ends]
The Indentation Is Important
Notice:
for fruit in fruits: print(fruit)The print statement is indented. That tells Python: "This statement belongs to the loop."
Without indentation, the structure would be invalid.
You can put multiple statements inside the loop:
for fruit in fruits: print("I found a fruit") print(fruit)Python executes both statements for every item.
Output:
I found a fruit
Apple
I found a fruit
Banana
I found a fruit
Mango
Looping Through Numbers
Lists do not have to contain strings. You can have:
numbers = [10, 20, 30, 40]for number in numbers: print(number)Output:
10
20
30
40
You can also perform calculations:
numbers = [10, 20, 30]for number in numbers: print(number * 2)Output:
20
40
60
The loop takes each number and performs the same operation.
A Real-World Example
Imagine an online store with a list of prices:
prices = [100, 250, 75, 300]Suppose you want to display each price. You can write:
for price in prices: print(price)Now imagine you want to add a tax calculation:
for price in prices: tax = price * 0.18 print(tax)The same logic is applied to every price. This is the real power of loops: Write the logic once, apply it to many items.
Lists Can Be Modified
Python lists are mutable, meaning their contents can be changed.
For example:
fruits = ["Apple", "Banana", "Mango"]You can replace an item:
fruits[1] = "Orange"Now the list becomes conceptually: ["Apple", "Orange", "Mango"]
You can also add items using methods such as:
fruits.append("Grapes")Now: ["Apple", "Orange", "Mango", "Grapes"]
You will learn list methods in greater detail later.
A Powerful Mental Model
Whenever you see:
for item in collection: do_something(item)translate it in your head as: "Take each item from the collection, one at a time, and perform this operation."
For example:
for student in students: print(student)means: "Take each student from the students collection and print that student."
This mental translation will help you understand loops much faster.
List + Loop = A Powerful Combination
Lists store collections. Loops process collections.
Together:
LIST (Contains many values)
↓
FOR LOOP (Takes values one by one)
↓
Performs an operation
For example:
tasks = ["Study", "Exercise", "Code"]for task in tasks: print("Today's task:", task)Output:
Today's task: Study
Today's task: Exercise
Today's task: Code
You wrote the operation once, but Python repeated it for every item.
[!TIP] Key Takeaways
- A Python list stores multiple values in an ordered collection.
- Lists use square brackets.
- Python uses zero-based indexing.
- The first item has index 0.
- A loop repeats an operation.
for item in list:means "process each item in the list."- The loop variable represents the current item.
- Indentation determines which statements belong to the loop.
- Lists and loops are among the most important building blocks in Python.
Beginner rule: When you see
for item in list, imagine Python handing you one item at a time until the list is finished.