3. Looping over a collection
Avoid using an index just to access elements in a collection.
Direct iteration emphasizes the values you care about instead of the mechanics of how to reach them. Index-driven loops are best reserved for cases where the position itself is part of the logic.
3.1. Don’t do this
1names = ['john', 'jane', 'jeremy', 'janice', 'joyce', 'jonathan']
2
3for i in range(len(names)):
4 print(names[i])
3.2. Do this
1names = ['john', 'jane', 'jeremy', 'janice', 'joyce', 'jonathan']
2
3for name in names:
4 print(name)