Getting the Python List Length is very useful and time-saving for the big Programs and real-world applications. Finding the length of a list in Python programming language is quite easy and time-saving.
To calculate the Python List Length we have generally four ways. The First one is quite easy and simple using the in-built len() function. Secondly, you can find the list length using the user-defined or you can say the naive method. The third way is by using the length_hint() method. And the final method is by using the __len__() method.
So now let’s start the tutorial with finding the Python length of the list using the len() function.
Finding Python List Length Using len() Function
Python len() is a built-in function in python. You can use the len() to get the length of the given list, string, array, tuple, dictionary, etc.
You can use the len function to optimize the performance of the program. The number of elements stored in the object is never calculated, so len helps provide the number of elements
Syntax of len() Function
Following is the basic syntax for len() function in Python:
len(List_name)
Parameters:
Parameter | Description |
---|---|
List_name | The list whose length will be counted |
Return value
It will return an integer value i.e. the length of the list.
Defining a List
Lists are written within square brackets []
Example:
# Define a list
sample_list = [3, 7, 4, 2]
Example 1: Finding Length Using len()
# Python program to find length of list
# using len() method
MyList = ['Red', 'Yellow', 'Green', 'Orange', 'Pink']
print("The Length of the list is: ", len(MyList))
Output:
The Length of the list is: 5
Example 2: Finding Length of Empty List Using len()
MyList = []
print("The Length of the list is: ", len(MyList))
Output:
The Length of the list is: 0
Finding Python List Length Using The Naive Method
In this naive method to find the length of list, we are taking the input from the user. And counting the number of characters in the input string using for loop
Example: Finding Length Using Naive Method
MyList = ['Red', 'Yellow', 'Green', 'Orange', 'Pink']
count=0
for i in MyList:
count=count+1
print("List:",MyList)
print("Number of Elements:",count)
Output:
List: ['Red', 'Yellow', 'Green', 'Orange', 'Pink']
Number of Elements: 5
In this naive method, we use the count variable to find the number of elements in the list. It is technic of using collection controlled loop format (for). In this collection, controlled loops are not following any condition it follows only the length of the collection. For that, we just add the value of count by 1. And finally, it returns the number of elements.
Finding Python List Length Using The __len__() Method
__len__() method is another built-in method available in python for finding the length of the list.
Though len() and __len__() method looks similar there is the slightest of difference in these two.
When len() function is called it internally calls the __len__() method which returns the counts of items in the sequence i.e the list. Also, we can directly call the __len__() method.
Example: Program To Find Length Using __len__() Method
# Python program to find length of list
# using __len__() method
# Initializing list to find length
MyList = ['Red', 'Yellow', 'Green', 'Orange', 'Pink']
Length = MyList.__len__()
print("The Length of the list is: ", Length)
Output:
The Length of the list is: 5
Finding Python List Length Using The length_hint() Method
This technique is a lesser-known technique of finding length. This particular method is defined in operator class and it can also tell the no. of elements present in the list.
Definition
Returns with the length of iterable items.
Syntax
Operator.length_hint()
Parameters
objective
iterable
Examples
operator.length_hint(list)
The method returns with the length of the element in the list.
Example: Program To Find Length Using length_hint() Method
# Python program to find length of list
# using length_hint method
from operator import length_hint
# Initializing list to find length
MyList = ['Red', 'Yellow', 'Green', 'Orange', 'Pink']
print("The Length of the list is: ", length_hint(MyList))
Output:
The Length of the list is: 5
Must Read:
- Python Data Types
- Implementation of Stack in Python
- Substring operations in Python
- Best Book to Learn Python in 2020
The execution time of Various Methods to Find Length of List:
- len(): 0.0 (seconds)
- __len__() : 0.0 (seconds)
- length_hint() : 0.00046 (seconds)
- Naive method: 0.00057 (seconds)
Hence, len() and __len__() are the best methods to find the length of the list that we could use since it has the least execution time among all the available methods.
Point To Be Noted For Python Newbies
If you came from PHP / JavaScript, by array
, probably you mean list
in Python
Conclusion
This was all about finding the length of the list in Python. The len() method is the most popular method. Whereas, you can also use the other three methods of finding the length of the list.
With this, we have come to the end of our article. I hope you understood how to find the length of any list in Python.
Happy Coding!