Skip to content

Python Quickie: Make For Loops Faster

Make for loops faster can sometimes reduce waiting/ processing time and ressources. So why not apply it to python? Here are a couple of ways!

There’s an easy way to make for loops faster in python! You can push loops into compiled C code or use list comprehensions. Sounds interesting? It is! And faster. As mentioned before. So here’s how:

Let’s say you want to uppercase every String of a list. Normally you would do something like

Method 1 – Send loop into compiled C code

It’s not just faster in execution, it’s also faster to type 😉

The map() method pushes the loop into compiled C code. You Pass the str.upper method followed by a list (inputList). It also works with more. Read the link given before to find out more.

Method 2 – List comprehension

This one is also just a one-liner:

They are both an easy way to make for loops faster in python.

 

Published inQuickiesScripting/ Coding

Be First to Comment

Leave a Reply

%d bloggers like this: