In this article, we’ll discuss how list length can be found in Python and we’ll show you three different methods for determining it.
Using len() Function
The simplest and most straightforward way to find the length of a list is by using the built-in len() function. The len() function returns the number of items in a sequence, so it can be used on any type of sequence (e.g., lists, tuples, strings).
So if you have a list called L and want to know how many items are in it:
L = [1, 2] # Create a new list
len(L) # Returns 1 because there is only one item in the list
Using size() Function
The size() function returns the number of items in a list.
If the list is empty, then it will return 0.
It also returns -1 if the list is empty.
This function is faster than len() because it doesn’t need to check every element of an iterable to find out how many elements are there in it. But this function cannot be used with Python 3 because size() has been removed from that version of Python and replaced by length().
Using range(len(mylist)) Function
In this example, the range() function is used to iterate through a list. The argument is the length of the list and the return value is always a list. You can use it as a generator or generator expression to create a sequence of numbers (like in for loops), as well as for comprehension and other things.
The length of list in Python is a very important concept and it is crucial to understand how to find the length of a list. The three methods given here will help you learn how to find the length of the list in Python.