sethserver / Python

Python: How For Loops Work

Python has some pretty amazing features, and one of its most powerful and versatile is the for loop.

pythons = ['Graham', 'John', 'Terry G', 'Eric', 'Terry J', 'Michael',] for name in pythons: print(f"It's {name}")

Simple enough: a list of six names and a for/in loop that iterates over the names in the list and outputs each name on a line. I feel like it's important right out of the gate to point out a caveat that trips up a lot of Python programmers: variable scoping. Let's try this again but print out our variable name outside of the loop.

pythons = ['Graham', 'John', 'Terry G', 'Eric', 'Terry J', 'Michael',] for name in pythons: print(f"It's {name}") print(f"Still {name}")

If you run this code you'll notice something that is unexpected. The variable name is still in scope and holds the last value from the for loop. Keep this in mind because it can cause some strange things to happen if you reuse variable names in your code.

The next feature that makes Python's iterators so powerful is the generator. Generators allow lazy-loading of iterables, which means you are not required to front-load long running, IO-bound, CPU or memory intensive tasks. The following example will iterate over a million strings using a generator.

def large_memory_list(): for n in range(1000000): yield('thisisasuperduperlongstringnotreallybutletusjustpretendforamomentthatitisinfactalongstringthankyou') for long_string in large_memory_list(): print(f"{long_string}")

Instead of allocating a million strings at once, Python allows us to generate the strings one at a time using the yield statement. This can be useful for processing large quantities of data one line at a time, or when iterating over a large list of items when you don't necessarily need to traverse the entire set. As you can see the for loop in Python is quite powerful when used in conjunction with native lists, sets, and generators.

Good luck and happy looping!