Posts Tagged ‘Enumerate’

How to iterate through a list using an index in Python?

Iterating through a list in Python is simple using a for in loop. colours = ["red","green","blue"]   for colour in colours: print colour To iterate through the same list using an index we can apply the enumerate() function. colours = ["red","green","blue"]   for i, colour in enumerate(colours): print i, colour