How to Find Length of List in Python?

Posted in /  

How to Find Length of List in Python?
vinaykhatri

Vinay Khatri
Last updated on December 25, 2024

    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 len() 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.

    Python 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.

    Example

    my_list = [1,2,3,4,5]
    print(my_list[0])
    print(my_list[4])

    Output

    1
    5

    How to Find Length of 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

    1. Python len() method

    len() 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.

    Example

    my_list = ["Welcome", "to", "techgeekbuzz",101]
    print(len(my_list))

    Output

    4

    2. Naive Method

    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.

    Example

    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))

    Output

    4

    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.

    Conclusion

    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:

    Leave a Comment on this Post

    0 Comments