Stop using For Loop instead Use this.

Aman Khan
2 min readAug 10, 2022

Hi Dev’s👋🏻, So today the Title could be more different like How to stop getting rants from Senior Developers in Code Reviews?

But Why I chose a different Title😅?

Because when I was a beginner in Freelancing, expert Developers rant me from all over the world for using For Loop in Production grade code.

Then this happened.

A very Good Developer which is now a friend of mine taught me some basic things which I missed out learning from YouTube😁.

Yeah I am also with no CS Degree🙂.

So he taught me to become a Senior in Anything, you need to know the Pros and Cons of what you are using.

If you need to do iteration then why there are multiple options available you need to know that. By — Him

So that when i started to run the experiments here is an example you can try, to avoid getting rant in Code Reviews👶.

List Comprehensions versus For Loops

So let’s checkout the time⌚. I used Google Collab so you can also replicate the same behavior with Runtime Type GPU. Here is the screenshot💻:

As we can see, the for loop is slower than the list comprehension (15.83 seconds vs. 8.46 seconds)😮.

The result will come similar when you run it on Google Collab not on your machine because then we would have different machine powers which would yield different result.

So conclusion

List comprehensions are faster than for loops to create lists.

This is because we are creating a list by appending new elements to it at each iteration. This is slow⏳.

When you are using frameworks like Django you will get Aggregation methods built in it. So you don’t have to use Loops and That’s how made my to become a Senior Developer😃.

So, Stay Tuned and I will see you in the next one👋🏻.

Thanks for Following and Claps👍!

--

--

Aman Khan

Blockchain and Web3 Engineer Crafting compelling stories at the intersection of tech, business, and culture.