The for loop in Python is a powerful and flexible construct that allows us to iterate over different types of data structures, including lists. In this blog post, we’ll explore different code examples that demonstrate how to use the for loop to iterate through a list efficiently.
Example 1:
fruits = ['apple', 'banana', 'kiwi', 'orange'] for fruit in fruits: print(fruit)
Output:
apple
banana
kiwi
orange
Example 2:
numbers = [1, 2, 3, 4, 5] for i in range(len(numbers)): print(numbers[i])
Output:
1
2
3
4
5