DEV Community

PRANTA Dutta
PRANTA Dutta

Posted on

Python Sucks at For Loops – And That’s Exactly Why We Love It

Let’s face it: Python is the cat of programming languages. It’s elegant, independent, and pretends it doesn’t need you (until it does). And nowhere is this attitude more apparent than in its for loops, which can simultaneously make you feel like a genius and an idiot at the same time.

Here’s the thing: Python doesn’t suck at for loops because they’re bad. It sucks at for loops because it’s too good at pretending to know better than you.

1. The Zen of Python: “Do Less Work, Fool”

In most programming languages, for loops are a straightforward concept. You want to count to 10? Fine, here’s some boilerplate to make you feel smart.

Here’s how you’d do it in C:

for (int i = 0; i < 10; i++) {
    printf("%d\n", i);
}
Enter fullscreen mode Exit fullscreen mode

Clean. Predictable. Respectful.

Now, Python, on the other hand, says:

“Why bother defining i, specifying a range, or doing basic arithmetic? I’ll just hand you everything on a silver platter and let you feel like an impostor programmer.”

Here’s Python’s version:

for i in range(10):
    print(i)
Enter fullscreen mode Exit fullscreen mode

That’s it. No declarations. No curly braces. Just vibes. Python’s for loops are so easy, they feel wrong.


2. But Python Doesn’t Stop There: It Wants You to Do Even Less

Let’s say you have a list of fruits and you want to print them out.

C Version (Again, Respectful):

char* fruits[] = {"apple", "banana", "cherry"};
for (int i = 0; i < 3; i++) {
    printf("%s\n", fruits[i]);
}
Enter fullscreen mode Exit fullscreen mode

Python Version:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
Enter fullscreen mode Exit fullscreen mode

Notice something? Python doesn’t even bother giving you the index. It just throws the entire element at you like a frisbee and says, “Here, deal with it.”

You wanted to feel smart by manually indexing your list? Too bad. Python already knows what you want and spoon-feeds it to you.


3. The List Comprehension Power Move

Python’s list comprehensions are where for loops go to die.

Want to create a new list where every number is doubled? In any other language, that’s a solid 3-4 lines of code. Python just casually flexes its one-liner game:

doubled = [x * 2 for x in range(10)]
Enter fullscreen mode Exit fullscreen mode

Not only is this efficient, but it also makes you feel like you’re writing code in some secret programming dialect that mere mortals will never understand. But it comes at a cost: your for loops now look like cryptic crossword puzzles.

Example:

results = [f"Employee-{i}" for i in range(10) if i % 2 == 0]
Enter fullscreen mode Exit fullscreen mode

Congratulations! You just wrote a line of code that nobody—including you—will understand two weeks from now.


4. “Let’s Break Things for Fun”

Python’s for loops also love to betray you in subtle ways. Here’s a classic mistake:

The Accidental Variable Overwrite

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    fruit = "pineapple"

print(fruits)  # ["apple", "banana", "cherry"]
Enter fullscreen mode Exit fullscreen mode

Wait, what? Didn’t we replace everything with "pineapple"? Nope! Python is over here laughing in the corner because fruit was just a temporary variable. The actual list is untouched.

Meanwhile, JavaScript devs are smirking, knowing they can break entire production systems with variable scope issues.


5. Enumerate: The MVP We Don’t Deserve

At some point, you’ll want both the index and the value in your loop. Python could’ve just let you use a boring i like the other kids. Instead, it hands you enumerate(), which sounds more like a corporate jargon term than a programming function.

fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")
Enter fullscreen mode Exit fullscreen mode

“Enumerate.” Really? Python, this isn’t a board meeting. Relax.


6. Python Loops Just…Stop Working

Want to modify a list while looping through it? Python will just look at you, deadpan, and go:

“You thought.”

Example:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num % 2 == 0:
        numbers.remove(num)

print(numbers)  # [1, 3, 5]
Enter fullscreen mode Exit fullscreen mode

Now the list is missing elements because Python got confused halfway through. If Python were a waiter, this would be the equivalent of them clearing your table before you’re done eating.


7. The Infinite Loop That Isn’t

Try writing a classic infinite loop in Python. You know, for fun. Here’s what it looks like in C:

for(;;) {
    printf("Still going!\n");
}
Enter fullscreen mode Exit fullscreen mode

Now here’s Python’s version:

while True:
    print("Still going!")
Enter fullscreen mode Exit fullscreen mode

It works, sure, but it feels weird. Python doesn’t even try to look like a classic infinite loop. It’s just...an endless truth.


Conclusion: Python’s For Loops Don’t Suck—We Do

The truth is, Python doesn’t actually suck at for loops. We just suck at realizing how spoiled we’ve become. Python’s loops are so intuitive, so minimalist, and so powerful that we forget the pain of manually tracking indices or dealing with segmentation faults.

So the next time you’re grumbling about Python’s for loops, remember this: Python doesn’t suck. It’s just tired of holding your hand.

Top comments (1)

Collapse
 
john_matthew_82c0880ee1c1 profile image
John Matthew

nice one