How to get the size of a list in Python?


In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array.

In this article, we will show you how to get the size/length of a list in different ways using Python. Here we see 4 methods −

  • Using len() function

  • Using For Loop (Naïve Method)

  • Using length_hint() function

  • Using __len__() function

Assume we have taken a list containing some elements. We will return the length/size of the given input list using different methods as specified above.

Method 1: Using len() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Get the length/size of the list using the len() function (The number of items in an object is returned by the len() method. The len() function returns the number of items in a list when the object is a list) and create a variable to store it.

  • Print the length/size of a list.

Example

The following program returns the size of the list using the len() function−

# input list lst = ["Hello", "TutorialsPoint", 78, "Hi", "Everyone"] # getting list length listLength = len(lst) # Printing the size of a list print("List=",lst) print("Size of a List = ", listLength)

Output

On executing, the above program will generate the following output −

list= ['Hello', 'TutorialsPoint', 78, 'Hi', 'Everyone']
Size of a List = 5

We were given a list with some random data on it. We used the len() method to calculate the length of the given list and displayed the result

Method 2: Using For Loop(Naive Method)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Create a variable to store the length of the list by initializing it with 0 at first

  • Use the for loop, to traverse through each element of the list.

  • Increment the length of the list variable by 1 for each iteration

  • Print the length/size of a list after the end of for loop.

Example

The following program returns the size of the list using the for loop (Naïve method)

# input list lst = ["Hello", "TutorialsPoint", 78, "Hi", "Everyone"] # Storing the length of the list by initializing with 0 at first listLength = 0 # Traversing each element of the list for element in lst: # incrementing the listLength variable listLength = listLength + 1 # Printing the size of a list print("List = ",lst) print("Size of a List = ", listLength)

Output

On executing, the above program will generate the following output

List = ['Hello', 'TutorialsPoint', 78, 'Hi', 'Everyone']
Size of a List = 5

We were given a list containing some random data. We chose a variable to hold the length of the list and initialized it with 0, then traversed through each element of the list and increased the value of list length by 1 inside the loop, and finally showed the length of the list.

Method 3: Using length_hint() function

The length_hint() method is a less well-known method for determining the length of a list or other iterable.

length_hint() is defined in the operator module, therefore you must import it before using it.

length_hint(list)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the length_hint function from the operator module.

  • Create a variable to store the input list and add some dummy data to it.

  • Get the length/size of the list using the length_hint() function by passing the input list as an argument to it and create a variable to store it.

  • Print the length/size of a list.

Example

The following program returns the size of the list using the length_hint() function −

# importing length_hint function from operator import length_hint # input list lst = ["Hello", "TutorialsPoint", 78, "Hi", "Everyone"] # getting list length listLength = length_hint(lst) # Printing the size of a list print("List = ",lst) print("Size of a List = ", listLength)

Output

On executing, the above program will generate the following output

List = ['Hello', 'TutorialsPoint', 78, 'Hi', 'Everyone']
Size of a List = 5

After importing the length hint() method from the operator module, we were given a list containing some random data. We used length hint() to determine the length of the list and displayed it.

Method 4: Using __len__() function

The __len__ function in Python returns a positive integer representing the length of the object on which it is called. It implements the inbuilt len() function.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Get the length/size of the list by applying the __len__() function on the input list and create a variable to store it.

  • Print the length/size of a list.

Example

The following program returns the size of the list using the __len__() function −

# input list lst = ["Hello", "TutorialsPoint", 78, "sample", "python", "code"] # getting list length using the __len__() function listLength = lst.__len__() # Printing the size of a list print("Size of a List = ", listLength)

Output

On executing, the above program will generate the following output −

Size of a List = 6

We were given a list with some random data in it, and we used the __len__() function to get the length of the list. Finally, we displayed the length of the list as determined by the above function return value.

Conclusion

We learned four different methods for calculating the length of a list. In this article, we learned about the functions len(), length_hint(), and __len__(). We also learned how to use the for loop and the in operator to traverse over each element of the list.

Updated on: 09-Sep-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements