Outside Resources:
11 Powerful Methods to Iterate Through Lists
(includes some more advanced content – save for later)
You can use for loops together with lists to make your code shorter and easier to change.
Let’s say you want to print the three letter abbreviation of each word in a list of days of the week:
days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
Here is a way to do it by combining the len() and range() functions:
for i in range(len(days)): print(days[i][0:3])
len(days) is 7, so range(len(days)) becomes range(7)
Combining range() and len() causes your for loop to repeat once for every item in the list.
Notice days[i] in the code: this equals one of the items in your list. Since the variable i is changing each time through the loop, we get a different item from the days list each time the loop repeats.
The same code still works if you change the number of items in the list, unlike the following example:
for i in range(7): print(days[i])
Unfortunately, this program will break if you remove ‘Saturday’ and ‘Sunday’ to convert it to a list of only weekdays:
days = ['Monday','Tuesday','Wednesday','Thursday','Friday'] for i in range(7): print(days[i])
Output:
Traceback (most recent call last): File "python", line 3, in IndexError: list index out of range
“Index out of range” happens because eventually i = 5, but the list only goes up to item 4 (five week days, five index numbers: 0,1,2,3,4).
For Each Loops
The previous example uses a stepper variable to iterate through the list. We can avoid that variable by using a walker variable in a for each loop:
woods = ['pine', 'oak', 'maple', 'balsa'] for w in woods: print(w)
Notice that instead of woods[i], the code simply prints w; w is a walker variable. It’s named w since that’s the first letter of woods. It would have also been named something like “wood” (singular) if the programmer feels that makes the code easier to read.
If you wanted to print the first letter of each word in the list, you could do it like so:
woods = ['pine', 'oak', 'maple', 'balsa'] for w in woods: print(w[0])
An advantage of this type of loop is that it shortens your code. A disadvantage is that the item number is no longer available like it was before.
Bonus: enumerate()
Sometimes you want to use a walker variable AND you want to have the item number available in your code. Here’s a way to do that:
woods = ['pine', 'oak', 'maple', 'balsa'] for i, wood in enumerate(woods): print(i,wood)
Output:
0 pine 1 oak 2 maple 3 balsa
In this example, the variable i is the item number and wood is the actual item from the list.
As a random example, maybe you want to print the first letters and the last letter of each word, but only for every second word. The following example does that with a small amount of code. It uses the counter variable, i, to do every second word (for even numbers i%2 is zero). The walker variable, wood, is used to print only certain letters.
woods = ['pine', 'oak', 'maple', 'balsa'] for i, wood in enumerate(woods): if i%2==0: print(wood[0:2] + wood[-1])