For Each Item Loop

Because it is very common to use a for loop to touch each of the elements of a collection, we can do it without even using a range and index. We can use a for loop that repeats the body of the loop one time for each value in the collection. This is sometimes called a for each loop since it repeats the body of the loop one time for each element in the collection. It works for both strings and lists as shown below.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

This isn’t actually a different for loop. The function range actually creates a list of numbers, and the index just steps through each of those items one at a time.

You have attempted 1 of 4 activities on this page