Report this

What is the reason for this report?

How To Find the Length of a List in Python

Updated on March 21, 2024
Safa MulaniAndrea Anderson

By Safa Mulani and Andrea Anderson

How To Find the Length of a List in Python

Introduction

There are several techniques you can use in Python to find the length of a list. The length of a list is the number of elements in the list.

To get the length of a list in Python, the most common way to do so is to use the len() method as it’s the most efficient and is a built-in function. Since a list is an object, the size of the list if already stored in memory for quick retrieval.

We have also included two other methods you can use to find the length of a list with python.

Different Python Methods to Find length of a list

  1. Python Len Method
  2. length_hint Method
  3. Python For Loop Method

Using the len() method to get the length of a list

You can use the built-in len() method to find the length of a list.

The len() method accepts a sequence or a collection as an argument and returns the number of elements present in the sequence or collection.

The syntax of len() is:

len(s)

The following example provides a list and uses the len() method to get the length of the list:

inp_lst = ['Python', 'Java', 'Ruby', 'JavaScript']
size = len(inp_lst)
print(size)

The output is:

Output
4

Alternative Ways to Find the Length of a List

Although the len() method is usually the best approach to get the length of a list because it’s the most efficient, there are a few other ways to find the length of a list in Python.

Using the length_hint() method to get the length of a list

The Python operator module has a length_hint() method to estimate the length of a given iterable object. If the length is known, the length_hint() method returns the actual length. Otherwise, the length_hint() method returns an estimated length. For lists, the length is always known, so you would normally just use the len() method.

The syntax of length_hint() is:

length_hint(object)

The following example provides a list and uses the length_hint() method to get the length of the list:

from operator import length_hint 
inp_lst = ['Python', 'Java', 'Ruby', 'JavaScript']
size = length_hint(inp_lst)
print(size)

The output is:

Output
4

Using for loop to get the length of a list

This section provides a less practical, but still informative, way of finding the length of a list with no special method. Using a python for loop to get the list length is also known as the naive method and can be adapted for use in almost any programming language.

The basic steps to get the length of a list using a for loop are:

  • Declare a counter variable and initialize it to zero.

    counter = 0
    
  • Use a for loop to traverse through all the data elements and, after encountering each element, increment the counter variable by 1.

    for item in list:
      counter += 1
    
  • The length of the array is stored in the counter variable and the variable represents the number of elements in the list. The variable can be used in other code or output.

    print(counter)
    

The following example demonstrates how to get the length of a list:

inp_lst = ['Python', 'Java', 'Ruby', 'JavaScript']
size = 0
for x in inp_lst:
    size += 1
print(size)

The output is:

Output
4

Conclusion

In this article, you learned some different ways to find the length of a list in Python. Continue your learning with more Python tutorials.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Safa Mulani
Safa Mulani
Author
Andrea Anderson
Andrea Anderson
Editor
Technical Editor
See author profile
Category:
Tags:

Still looking for an answer?

Was this helpful?
Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.