Python Tutorial

Iterate a Python List With Example

How to Iterate a Python List?

Using a For loop, you can iterate over a list in Python.

Example:

thislist = ["pear", "melon", "orange"]

for x in thislist:

  print(x)

Output:

pear
melon
orange

Loop Through the Index Number

You can also use the index number to loop through a list in Python. To make an appropriate iterable, use the range() and len() functions.

Example: 

thislist = ["pear", "melon", "orange"]

for i in range(len(thislist)):

  print(thislist[i])

thislist = ["pear", "melon", "orange"]

for i in range(len(thislist)):

  print(thislist[i])

Output: 

pear
melon
orange

Using a While Loop

This loop is used to loop through the list items. Use the len() method to retrieve the list's length, then start at 0 and work your way through the items by referencing their indexes. Remember to increase the index by one after each cycle.

Example:

thislist = ["pear", "melon", "orange"]

i = 0

while i < len(thislist):

  print(thislist[i])

  i = i + 1

Output:

pear
melon
Orange

Video : https://youtu.be/0zApRpidU58

Video : https://youtu.be/101WAhV1atg

Did you find this article helpful?