Getting the length of a list is a common operation in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to determine the length of a list efficiently.
Example 1:
fruits = ['apple', 'banana', 'orange', 'kiwi'] length = len(fruits) print(length)
Output: 4
Example 2:
numbers = [1, 2, 3, 4, 5] length = 0 for _ in numbers: length += 1 print(length)
Output: 5