Python (Scripting) - Looping over a list
by
Jeremy Canfield |
Updated: November 06 2023
| Python (Scripting) articles
Here is one way to loop through the items in a list.
for item in ["apple", "banana", "orange", "grapes"]:
print(item)
This should print each item in the list.
apple
banana
orange
grapes
However, it's more common to store the items in a list.
fruits = ["apple", "banana", "orange", "grapes"]
You can print the list. This will display the entire list.
print(fruits)
Which should return the following.
['apple', 'banana', 'orange', 'grapes']
You can loop through the list.
for item in fruits:
print(item)
This should print each item in the list.
apple
banana
orange
grapes
You can loop through the list in reverse order.
for item in reversed(fruits):
print(item)
This should print each item in the list.
grapes
orange
banana
apple
Did you find this article helpful?
If so, consider buying me a coffee over at