Skip to content
Home » Python List Length | How to Find the Length of List

Python List Length | How to Find the Length of List

In this tutorial let’s learn about How to Find the Length of List in Python.

In Python, a list is an ordered and mutable collection datatype. A list can also contain duplicate entries. The len() function in Python is used to determine the length of any object. In this post, we will learn how to find the length of a list in Python by going through the following methods:

Python list length using len() Method

In Python, use the len() function to determine the length of a list. The len() function in Python returns the length of a list based on the total number of entries in the list. The len() method is the most commonly used and simple method for determining the length of any list.

Syntax:

len(list)

The length of the given list is returned by the len() function, which takes the list name or name of the list as an input.

Code:

sample_list = ['Similar','Geeks','Python','List','length']

length = len(sample_list)
print("The length of the list is:",length )

Output :

The length of the list is: 5

Python List Length using for loop

In Python, the len() function is the most often used way for determining the length of a list. However, there is another fundamental approach for determining the length of the list.

In the Naive technique, a loop is executed and the counter is increased until the last member of the list is reached to determine its count. In the absence of other effective strategies, this is the most basic tactic that can be applied.

Code Example :

sample_list = ['Similar','Geeks','Python','List','length']

length = 0

for i in sample_list:
    length = length + 1

print("The length of the list is:",length )

Output :

The length of the list is: 5

Find Length of an Array in Python

To find a length of an array in Python, use the len() function. To deal with an array in Python, import the array module then create an array with some elements using the array() method and then find the length of that array.

Code Example :

from array import array 

# array()
length = array('b',[2,5,1,5])

print("Number of elements in an Array :", len(length))

Output :

Number of elements in an Array : 4

Python List Length using length_hint() Method

The length_hint() function in the Python operator module is used to compute the total number of entries in the list.

The operator.length_hint() method is used to determine the length of an iterable such as a list, tuple, dict, and so on.

Syntax:

length_hint(iterable)

Code Example :

from operator import length_hint
sample_list = ['Similar','Geeks','Python','List','length']

length = length_hint(sample_list)

print("The length of the list is:",length )

Output :

The length of the list is: 5

Conclusion

In this tutorial we learnt different ways to find the Length of List in Python. Out of all the ways listed above, programmers believe Python’s built-in len() method to be the best approach for determining the size of the list. The len() function takes O(1) time to calculate the size of the list since the list is an object with memory space to store the size.

Similar Posts:

How To Convert Python List To CSV File | Python
Python Program to Sort list of tuple by Nth element of tuple
How to Convert a List to String in Python