For Loop In Python With List

4 min read

A for loop in Python with a list is one of the most common and useful ways to repeat an action for each item in a collection. Consider this: lists store multiple values in a single variable, and a for loop lets you process each element one at a time without manually writing the same code again and again. Whether you are adding numbers, checking conditions, printing values, updating items, or creating a new list, understanding how to loop through a list is essential for writing clean and efficient Python code It's one of those things that adds up..

Introduction to For Loops in Python

In Python, a for loop is used to repeat a block of code several times. When working with lists, the loop usually goes through each item in the list and performs an action on it Small thing, real impact..

A basic Python for loop has this general structure:

for item in list_name:
    # code to repeat

For example:

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

In this example, the variable fruit takes the value of each item in the list one at a time. The print() function is executed three times, once for each item.

How a For Loop Works with a List

A Python list is an ordered collection of values. These values can be numbers, strings, booleans, or even other lists. When you use a for loop with a list, Python automatically moves through the list from the first item to the last item Which is the point..

Example:

numbers = [10, 20, 30, 40]

for number in numbers:
    print(number * 2)

Output:

20
40
60
80

Here, each number is multiplied by 2, and the result is printed.

The important idea is that the loop variable, in this case number, changes value during each iteration. An iteration means one complete cycle of the loop.

Basic Syntax of a For Loop with a List

The syntax is simple:

for variable in list:
    statement

For example:

colors = ["red", "green", "blue"]

for color in colors:
    print("The color is", color)

Output:

The color is red
The color is green
The color is blue

The for keyword starts the loop. Because of that, the variable after for is created temporarily and receives each item from the list. So naturally, the word in connects the variable to the list. The indented block of code is what gets repeated.

Looping Through a List of Numbers

Lists often contain numbers, and for loops are commonly used to perform calculations on them.

Example:

temperatures = [22, 25, 19, 30, 27]

for temp in temperatures:
    print("Temperature:", temp)

You can also calculate totals, averages, or modified values.

prices = [12.50, 8.00, 15.25, 6.75]

total = 0

for price in prices:
    total += price

print("Total:", total)

Output:

Total: 42.5

This is a common pattern in Python programming: initialize a variable, loop through a list, update the variable, and use the final result That's the part that actually makes a difference..

Looping Through Strings in a List

A list can contain strings, and a for loop can print, search, or modify each string Simple, but easy to overlook..

Example:

names = ["Alice", "Bob", "Charlie"]

for name in names:
    print(name.upper())

Output:

ALICE
BOB
CHARLIE

You can also check whether a condition is true for each item:

words = ["python", "java", "javascript", "go"]

for word in words:
    if word.startswith("java"):
        print(word)

Output:

java
javascript

The startswith() method checks whether a string begins with a specific text. In this example, both "java" and "javascript" match the condition Surprisingly effective..

Using enumerate() to Get Index and Value

Sometimes you need both the value and the position of each item in a list. Python provides the built-in function enumerate() for this purpose.

fruits = ["apple", "banana", "cherry"]

for index, fruit in enumerate(fruits):
    print(index, fruit)

Output:

0 apple
1 banana
2 cherry

Python list indices start at 0, so the first item has index 0, the second has index 1, and so on Nothing fancy..

If you want indices to start at 1, use the start parameter:

scores = [88, 92, 75]

for position, score in enumerate(scores, start=1):
    print("Student", position, "score:", score)

Output:

Student 1 score: 88
Student 2 score: 92
Student 3 score: 75

Using enumerate() is often better than manually creating a counter.

Modifying a List with a For Loop

You can change items in a list while looping through it, but you usually need to use the index.

Example:

numbers = [1, 2, 3, 4]

for index in range(len(numbers)):
    numbers[index] = numbers[index] * 2

print(numbers)

Output:

[2, 4, 6, 8]

Here, range(len(numbers)) creates a sequence of indices from 0 to 3. The loop uses each index to update the corresponding list item.

You can also use enumerate() when you need both the index and the current value:

numbers = [5, 10, 15]

for index, value in enumerate(numbers):
    numbers[index] = value + 10

print(numbers)

Output:

[15, 20, 25]

Important Warning: Do Not Change List Size While Looping

A common mistake is adding or removing items from a list while looping through it. This can cause unexpected behavior because the list is changing while Python is trying to

Right Off the Press

Recently Completed

Fits Well With This

A Few More for You

Thank you for reading about For Loop In Python With List. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home