8. Looping over a collection with indices

The key here is to use enumerate which will return the index with the element.

8.1. Don’t do this

1names = ['john', 'jane', 'jeremy', 'janice', 'joyce', 'jonathan']
2
3for i in range(len(names)):
4    print(i, names[i])

8.2. Do this

1names = ['john', 'jane', 'jeremy', 'janice', 'joyce', 'jonathan']
2
3for i, name in enumerate(names):
4    print(i, name)