Iteration by Item
Whenever we execute a loop we have gone through an iteration. We can "iterate" (or loop) through the elements within certain data types. We can loop through the following data types as they are a collection of elements.
- Strings (a collection of characters)
- Lists (a collection of elements)
- Tuples (a collection of elements)
To iterate through a collection means to look at all of the elements within the collection. We can do this using the syntax shown below.
The variable "item" will be changed to equal the next item in the list after every loop (or iteration).
An example of when we may iterate by item is if we are checking to see if a certain item exists in a list.
We can do the same with strings or with tuples.