Refactor to Use a for Loop
00:09 So that doesn’t look clean or nice. And since you can see that we’re just doing the same thing over again, this usually is a good reason to use a loop. So I’m going to refactor this piece of code that’s working as I want it to into a loop.
00:34 Something that you can do in Python is just assign the loop variable to an underscore. This kind of signifies that, yeah, you’re going to use a loop variable, but you’re not going do anything with it in the loop.
So other people that write Python code are going to understand that. So I will say
for _ in range(3): So I want to do this three times. I want to run the loop three times, and then I want to pick out a noun,
Of course, now I don’t have noun one through three, so I need to also collect them somewhere. I’m going to create a new nouns list, start it off as an empty list, and then after removing them, I’m going to append to this new nouns list,
Become a Member to join the conversation.