11.5 Loop Lists

By | October 2, 2021

Loop Through a List

You can loop through the list items by using a for loop:

Example

Print all items in the list, one by one:
thislist = [“apple”, “banana”, “cherry”]
for x in thislist:
  print(x)

Output:
apple
banana
cherry

Loop Through the Index Numbers

You can also loop through the list items by referring to their index number.

Use the range() and len() functions to create a suitable iterable.

Example

Print all items by referring to their index number:
thislist = [“apple”, “banana”, “cherry”]
for i in range(len(thislist)):
  print(thislist[i])

Output:
apple
banana
cherry

Leave a Reply

Your email address will not be published. Required fields are marked *