Using .append() in "for" Loops With Additional Processing
For more information on concepts covered in this lesson, you can check out:
00:00
As you saw in the last lesson, a list comprehension is a great tool for building lists with specific requirements. Much simpler syntax than using a for
loop.
00:10
But there are times you can’t use a list comprehension, and that’s when you need to do additional work besides creating the list inside a loop. In that case, you need to go back to using the .append()
method inside a for
loop when building the list. Let’s take a closer look.
00:28 Remember, you’re not only building a list, but doing additional tasks while the list is being populated. Here’s an example. In this case, you want to keep track of how far along the loop is progressing as it computes the square roots. In this example, you want to create a list of square roots from a collection of numbers but you also want to display where in the process each step is at.
00:56
Notice here, the for
loop looks a little different, and not because we’re doing additional processing, but in this case, because of the specific processing this function wants to perform.
01:08
After it adds each square root to the list, it wants to display as a percentage how much of the argument collection
has been processed. To do this, it needs a variable to keep track of how much work has been done.
01:22
So, notice the difference in the for
loop header. It’s not for number in numbers:
like you saw in the first version.
01:32
It’s for i, number in enumerate(numbers):
. What’s happening here? The enumerate()
function does what you would expect it to. It numbers each of the items in the collection—in this case, it’s also called numbers
—starting with zero. It then returns a collection of tuples, each one unpacked into the variables i
and number
. There’s an excellent Real Python tutorial on enumerate()
if you’d like to learn more about it.
02:03
So, i
is holding each element’s position, and number
is holding that specific item from the original collection.
02:12
The function displays the number it’s currently at, adds its square root to the result
list, then computes how far it’s progressed through the list, having saved the length of the list to the variable n
before starting the loop.
02:31 You would not be able to perform all of this additional processing using list comprehension. Coming over to the terminal window, you can see this work.
02:46 This is version three. Here, you can see all of the outputs showing the progress of the function, and then the script prints the computed list, which is the same as before.
03:01
So there you’ve seen another example of using .append()
inside a for
loop to populate a list, this time when the loop was supposed to do additional processing and you weren’t able to use a list comprehension.
03:14
In the next set of lessons, we’ll look at how you can use .append()
with other list methods to implement common data structures such as stacks and queues.
Become a Member to join the conversation.