Lists in Python provide a means of storing multiple items in a single variable. In this tutorial we be iterating through a list in Python which will enable us to the items within the list and more.
We start by creating a list which can be seen below.
MyList = ['Stevie', 'Robin', 'Dustin', 'Will', 'Jim', 'Nancy', 'Mike']
To iterate the list using a for loop, we would do the following.
MyList = ['Stevie', 'Robin', 'Dustin', 'Will', 'Jim', 'Nancy', 'Mike']
for x in MyList:
print(x)
To iterate the list using a while loop, we would do the following.
MyList = ['Stevie', 'Robin', 'Dustin', 'Will', 'Jim', 'Nancy', 'Mike']
i = 0
while i < len(MyList):
print(MyList[i])
i = i + 1
the_ad id=’545′]
One thought on “Python Lists – Iterating lists”