First page Back Continue Last page Overview Graphics

Iterate over a List

fib = [0,1,1,2,3,5,8,13,21]

for el in fib:

print(el)

Sometimes it is necessary to access the indices as well:

fib = [0,1,1,2,3,5,8,13,21]

for i in xrange(len(fib)):

print(i,fib[i])

Alternative or “the Pythonic” approach:

>>> x = [34,65,89,78]

>>> list(enumerate(x))

[(0, 34), (1, 65), (2, 89), (3, 78)]

>>> for (index,value) in enumerate(x):

... print(index,value)