Bootstrap FreeKB - Python (Scripting) - Looping over a list
Python (Scripting) - Looping over a list

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 2f643a in the box below so that we can be sure you are a human.