A Python list can contain heterogeneous elements in sequential order. Unlike other programming-languages Python does not support built-in array data structure, but the list provides an alternative for it. Using the list, we can also store duplicate items.
In Python, we have an in-built function
which calculates the total number of elements present in a list and return the value. In this tutorial, we have mentioned the Python len() function in detail. And by the end of this tutorial, you will get a brief idea of how can you use len() function to find out the length of a list.
The list is a python built-in data structure that can store data elements in a sequence. And like arrays, the Python list uses indexing to access an individual element.
my_list = [1,2,3,4,5] print(my_list) print(my_list)
Find the length of a List in Python
In Python, we can use two techniques to find out the total number of elements present in a List.
- Python len() method
- Use naive method
Python len() method
is a Python in-built method, that accepts iterable objects as a parameter and returns the total number of elements parent in that iterable object. It is the most commonly used python method to find out the lent of a list or any iterable object.
my_list = ["Welcome", "to", "techgeekbuzz",101] print(len(my_list))
Generally, we use the len() method to find out the length of a string. But we can also write a user-defined function which would work similarly to the len() method. In the naive method, we use a loop and a counter variable. Using the loop we traverse through the list elements and increase the counter count.
def count(my_list): counter=0 for i in my_list: counter+=1 return counter my_list =["Welcome", "to", "techgeekbuzz", 101] print(count(my_list))
Python length of list
- The length of a list signifies the total number of elements present in a list.
- The index value of list starts from 0 upto n-1 (n is the total number of items).
- len() is the most commonly used python method.
- The len() method return the total items present in a list or other iterable.
So that was all about finding the length of a list in Python. We have discussed two methods to find the total number of elements in a Python list. The first method is the len() function, which makes it pretty easy to find the length of a Python list. The second method is the Naive method in which we use a loop to traverse through a Python list and a counter variable to increase the count after each element in the list. We hope you have understood how to find the length of a list in Python. Also, if you have any queries, feel free to share them in the comments section below.
People are also reading:
- While Loop in Python
- Python if, if…else, if…elif…else & Nested if Statement
- Input Output (I/O) and Import in Python
- Python Comments, Statements, Indentation & Docstring
- Variables, Constants and Literals in Python
- Learn Python Programming Language
- Data Structure in Python
- Python List or Array
- Best Python Books
- Python vs Django
Leave a Comment on this Post