Eliminating loops
1. Eliminating loops
In this lesson, we'll discuss the concept of looping. Although using loops when writing Python code isn't necessarily a bad design pattern, using extraneous loops can be inefficient and costly. Let's explore some tools that can help us eliminate the need to use loops in our code.2. Looping in Python
Python comes with a few looping patterns that can be used when we want to iterate over an object's contents. For loops iterate over elements of a sequence piece-by-piece. While loops execute a loop repeatedly as long as some Boolean condition is met. Nested loops use multiple loops inside one another. Although all of these looping patterns are supported by Python, we should be careful when using them. Because most loops are evaluated in a piece-by-piece manner, they are often an inefficient solution.3. Benefits of eliminating loops
We should try to avoid looping as much as possible when writing efficient code. Eliminating loops usually results in fewer lines of code that are easier to interpret. Recall in a previous lesson we referred to the Zen of Python - a collection of idioms for writing Pythonic code. One of these idioms was "flat is better than nested." Striving to eliminate loops in our code will help us follow this idiom. In the following examples, we'll see that there are often more efficient approaches that can be used instead of using a loop.4. Eliminating loops with built-ins
Suppose we have a list of lists, called poke_stats, that contains statistical values for each Pokémon. Each row corresponds to a Pokémon, and each column corresponds to a Pokémon's specific statistical value. Here, the columns represent a Pokémon's Health Points, Attack, Defense, and Speed.5. Eliminating loops with built-ins
We want to do a simple sum of each of these rows in order to collect the total stats for each Pokémon. If we were to use a loop to calculate row sums, we would have to iterate over each row and append the row's sum to the totals list. We can accomplish the same task, in fewer lines of code, with a list comprehension. Or, we could use the built-in map function that we've discussed previously.6. Eliminating loops with built-ins
Each of these approaches will return the same list, but using a list comprehension or the map function takes one line of code, and has a faster runtime.7. Eliminating loops with built-in modules
We've also covered a few built-in modules that can help us eliminate loops. Remember when we collected the different combinations of Pokémon types? Instead of using the nested for loop, we used combinations from the itertools module for a cleaner, more efficient solution.8. Eliminate loops with NumPy
Another powerful technique for eliminating loops is to use the NumPy package. Suppose we had the same collection of statistics we used in a previous example but stored in a NumPy array instead of a list of lists.9. Eliminate loops with NumPy
We'd like to collect the average stat value for each Pokémon (or row) in our array. We could use a loop to iterate over the array and collected the row averages. But, NumPy arrays allow us to perform calculations on entire arrays all at once. Here, we use the dot-mean method and specifying an axis equal to 1 to calculate the mean for each row (meaning we calculate an average across the column values). This eliminates the need for a loop and is much more efficient.10. Eliminate loops with NumPy
When comparing runtimes, we see that using the dot-mean method on the entire array and specifying an axis is significantly faster than using a loop.11. Let's practice!
Now, let's practice replacing loops with the techniques we've learned!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.