🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

for Loop

AI & DATA SCIENCE // for-loop

The for loop iterates over the items of any iterable — a list, string, dict, range, or generator — executing its body once per item.

Syntax

for item in iterable:
    # code runs once per item

Deep Dive Course

Unlike a C-style for loop that manually manages a counter, Python's for loop always iterates directly over an iterable's items, pulling them one at a time via the iterator protocol. To loop a specific number of times, you iterate over a range object; to get both index and value, wrap the iterable in enumerate(); and to loop over two sequences in parallel, wrap them in zip(). Any object that implements the iterator protocol can be looped over.

1Understanding for Loop

Unlike a C-style for loop that manually manages a counter, Python's for loop always iterates directly over an iterable's items, pulling them one at a time via the iterator protocol. To loop a specific number of times, you iterate over a range object; to get both index and value, wrap the iterable in enumerate(); and to loop over two sequences in parallel, wrap them in zip(). Any object that implements the iterator protocol can be looped over.

💡

Use enumerate() when you need an index inside a for loop, instead of manually incrementing a counter variable or indexing with range and len.

editor.html
colors = ["red", "green", "blue"]
for color in colors:
    print(color)
localhost:3000

2Practical Example

Here is a real-world application of for Loop showing how it is used in production Python code.

editor.html
for i in range(1, 6):
    if i % 2 == 0:
        print(f"{i} is even")
localhost:3000

3Best Practices

Follow these guidelines when working with for Loop:

1. Iterate directly over the collection instead of indexing with range and len

2. Use enumerate() when both the index and the value are needed

3. Use zip() to iterate over two or more sequences together instead of indexing each one separately

⚠️

Tip: Use enumerate() when you need an index inside a for loop, instead of manually incrementing a counter variable or indexing with range and len.

editor.html
colors = ["red", "green", "blue"]
for color in colors:
    print(color)
localhost:3000

Examples

Example 01Basic Usage
colors = ["red", "green", "blue"]
for color in colors:
    print(color)
Example 02Advanced Example
for i in range(1, 6):
    if i % 2 == 0:
        print(f"{i} is even")

Best Practices

  • Iterate directly over the collection instead of indexing with range and len
  • Use enumerate() when both the index and the value are needed
  • Use zip() to iterate over two or more sequences together instead of indexing each one separately

Interview Question

What actually happens under the hood when you write a for loop over a list?

Hint: Think about the iterator protocol.

Python calls iter() on the list, which returns an iterator object, then repeatedly calls next() on that iterator to pull one item at a time, assigning each result to the loop variable and running the loop body, until next() raises StopIteration, which ends the loop automatically. This same iterator protocol is what lets a for loop work identically on lists, strings, dicts, files, and any custom object that implements it.

Exercises

MediumPractice using for Loop in a real scenario.
View Solution
colors = ["red", "green", "blue"]
for color in colors:
    print(color)

Frequently Asked Questions

What actually happens under the hood when you write a for loop over a list?

Python calls iter() on the list, which returns an iterator object, then repeatedly calls next() on that iterator to pull one item at a time, assigning each result to the loop variable and running the loop body, until next() raises StopIteration, which ends the loop automatically. This same iterator protocol is what lets a for loop work identically on lists, strings, dicts, files, and any custom object that implements it.

Related Functions

while-loopenumerate()range()