Repetition

It is very common to loop through every value in a list. This is also referred to as traversing the list. Python allows a shortcut to perform this type of an operation, usually called a “for all” loop or a “for each” loop. Specifically, when L is a list, this code

for element in some_list:
  «loop body block»

Here is an example:

List Loop

You can also use multiple lists and a loop to copy elements from one list to another list.

List Copy

In the example above we used one list to hold all of the items that contained the letter “a”. There are many ways to do this exercise, but we find that this is the least bug-prone. Some students want to use remove, but we highly recommend you don’t do that and instead use a clean new list to store the items that you want.