Signup/Sign In
PUBLISHED ON: JUNE 30, 2021

Python Program To find length of list

List is an ordered set of values enclosed in square brackets [ ]. List stores values called elements in it, which can be accessed by their particular index. List is mutable, i.e. its values can be modified. List elements are the values inside the list. Since it has elements, it also has a size just like strings. The size of a list is equal to the total elements which are present in it.

In this tutorial, we will discuss different ways of finding the length of a list in Python.

Input: [1, 3, 6, 8, 0, 9]

Output: 6

Approach To Find Length

Since the total elements in a list are the size of the list, we can calculate all the elements in the list to find the size. This is a naive approach and is time-consuming for lists with large sizes. To optimize our program we can use len() function which is a built-in function and will return the size of the list.

We will discuss both approaches in detail in the following topics.

Approach 1: Naive Method

In the naive method, we simply count all the elements in a list using a loop and a counter variable. Run a loop for all the elements in the list and increase the counter variable till the last element is encountered. The value of the counter variable will be the length of the list.

Algorithm

Follow the algorithm to understand the working of the program

Step 1- Define a function to calculate the length

Step 2- Declare a counter and set it to 0

Step 3- Run a loop for elements in the list

Step 4- Increase the counter in each iteration

Step 5- Return the counter variable

Step 6- Initialise a list

Step 7- Pass the list in the function

Step 8- Print the value returned by the function

Python Program 1

Look at the program to understand the implementation of a naive approach

#length of list
#using loop

#function
def list_length(list):
    counter=0
    for i in list:
        counter=counter+1   
    return counter

# Initializing list 
li = [ 1, 4, 5, 7, 8 ]
  
# Printing list
print ("List : ",li)
 
# Printing length of list 
print ("Length of list using naive method is : ",list_length(li))


List : [1, 4, 5, 7, 8]
Length of list using naive method is : 5

Approach 2: Find Length Using len() Method

There is a built-in function called len() for getting the length of a list, tuple, array, dictionary in Python. The len() method takes a list as an argument and returns the length of the given list.

The len() function is an efficient and common method for finding the length of a list in Python.

Algorithm

Look at the algorithm to understand the implementation of the above approach

Step 1- Define a function for calculating the length

Step 2- Pass list as a parameter

Step 3- Return len(list)

Step 4- Initialise list

Step 5- Pass list in the function and display value

Program

This is the program for finding the length of a list using len() method.

#length of list using len()

#function
def list_length(list):
    return len(list)

#initialise list
li=[2,1,4,55,1,88,16,98]

# Printing list
print(li)

# Printing length of list
print("Length of list using len(): ",list_length(li))


[2, 1, 4, 55, 1, 88, 16, 98]
Length of list using len(): 8

Conclusion

In this tutorial, we have learned two ways of finding the length of a list. In the first approach, we have used a counter variable for counting the elements in the list. In the second approach, we have used a predefined function to return the length of the list.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.