howtouselinux

2 Ways to Find the Length of a List in Python

Table of Contents

Python is a versatile language that can be used for a variety of purposes.  How to find Python list length is a commonly asked question during a Python job interview. In this blog post, we will show you how to find the length of a list in Python.

This is a useful skill to have when you are working with data, and it only takes a few lines of code! Let’s get started!

Find the length of a list using len function in Python

The best way to find the length of a list in Python is using the len function. It accepts a list as an argument and returns the length of this list. It is a built-in function in Python. The len function is the most preferable, shortest, and Pythonic way to get the length of a list.

  • The len() function is O(1) in time complexity whereas using a loop is O(n).
  • It takes constant time and does not depend on the size of the list itself. This is possible because objects like lists track their own length and every time we use the len() function, it basically “looks up” for the length attribute of the object and returns it.

 

This is an example of getting the length of a list in Python.
list = [10, 20, 30]
n = len(list)
print(“The length of list is: “, n)
Output: 3

 

Find the length of a nested list using len function in Python

we can also use len function to get the length of a nested list.

A nested list is a list that is inside of another list. This can be useful for organizing data, or for creating complex lists. Nested lists can be as deep as you need them to be, and can include any number of levels.

You can use the len() function to get the total number of items in the nested list. You can also use the count() method to get information about a nested list. This method will return the number of times a certain value occurs in the list.

For example, the below code is finding the length of a nested list.
nestedList = [‘Krishna’, 20, ‘John’, [20, 40, 50, 65, 22], ‘Yung’, 11.98]
print(“\n Original List = “, nestedList)
print(“Length of a Nested List = “, len(nestedList[3]))
output:
Original List = [‘Krishna’, 20, ‘John’, [20, 40, 50, 65, 22], ‘Yung’, 11.98]
Length of a Nested List = 5

Find the length of a list of lists in Python

A list of lists means that it contains lists as elements. For example: [[1,2],[3,4]]. The first list in the list ( [1,2] ) is a one-dimensional list, and the second list ( [3,4] ) is also a one-dimensional list.

Each element in the outermost list is its own list. This means that you can access each element by using its index number. For example, to access the first element in the two-dimensional list, you would use the index number list [0]. We can use len function to get the length of a list of lists.

# Example to get the length of a list of lists
a_list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
print(len(a_list_of_lists))
# Returns: 3

 

Find the length of a list using for loop in Python

In order to get the length of a list in Python, using for loop is considered as a traditional technique. 

  • Declare a counter variable and initialize it to zero.
  • Using a for loop, traverse through all the data elements and after encountering every element, increment the counter variable by 1.
  • Thus, the length of the array will be stored in the counter variable. The variable will represent the number of elements in the list.

 

Example:
List = [‘Python’,’Java’,’Kotlin’,’Machine Learning’,’Keras’]
size = 0
print(“Length of the input string:”)
for x in List:
—-size+=1
—-print(size)
Output:
Length of the input string:
5

 

Reference:

3 Examples to Master Python list len() function

Understanding Python len function

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.