How to Find the Length of a List in Python?

A list data structure is used to store multiple ordered sequences of data inside the square bracket using comma-separated syntax. Values separated by commas inside the list are an element or item.

The number of items present in the given list is known as the length of the list. To calculate the length of the list, various methods are used. The most common method for calculating list length is the len() function.

This post will demonstrate a complete overview of how to get the list length in Python using the following aspects:

Let’s begin!

How to Get the List Length in Python?

To find the length of the list, an inbuilt “len()” function, “for loop” and “length_hint()” function of the operator module is used in Python. The size of a list refers to the number of elements/items contained in the list. List values are initialized inside the square bracket with comma-separated syntax, as shown below:

Let’s see the first method to find the length of a list in Python:

Method 1: Using len() Function

In the following example, the inbuilt “len()” function is used to get the Python list length.

Code:

#create list
list_value = ['Python','Guide', 'Linux', "Guide"]
#using len() function
result = len(list_value)
print('Length of the List: ',result)

In the above code:

  • The list is created and stored in a variable named “list_value”.
  • The “len()” function takes the variable as an argument and retrieves the list length.

Output:

In the above output, the length of the list is calculated successfully.

Method 2: Using For Loop

In the below example, the “for” loop iterates over the list and finds the length of the list using a simple counter.

Code:

#create list
list_value = ['Python','Guide', 'Linux', "Guide", 'Ubuntu']
#assign counter value
count = 0

print("Length of the List: ", end=' ')
#for loop iterates on the list
for element in list_value:
    count+=1
#printing the length of the list   
print(count)

In the above code:

  • The list containing string value as an element is initialized.
  • A variable named “count” is initialized with 0 in the program.
  • The “for” loop iterates on each element of the given list, and for each iteration, the “count” value is incremented with “1”.
  • In this way, the length of the list is calculated with the help of the “for” loop.

Output:

The length of the input list is found in the above output.

Method 3: Using length_hint() Function

In the example given below, the “length_hint()” function of the operator module is used to get the list length in Python.

Code:

from operator import length_hint
#create list
list_value = ['Python','Guide', 'Linux', "Guide", 'Ubuntu']
#using length_hint()
len_list = length_hint(list_value)
#printing length
print('Length of the list: ',len_list)

In the above code:

  • The “length_hint()” function is imported at the start of the program from the operator module.
  • The list is created and initialized with some values.
  • The “length_hint()” function takes the list variable as a parameter value and returns the length of the list.

Output:

As you can see from the above output, the list length is successfully calculated.

All right, that’s all for now!

Conclusion

To find the length of the list, an inbuilt function “len()”, the naive “for loop”, and the “length_hint()” function of the operator module is used in Python. The inbuilt “len()” function takes a list as a parameter value and retrieves the length of that list. Similarly, the “for loop” iterates over the list and increments the counter value by “1” on every iteration. The “lenght_hint()” is similar to the len() function, but to access this function, the operator module must be imported at the start of the program. This article presented an in-depth overview of how to find the list length in Python.