Tutorial

How To Find the Length of a List in Python

Updated on March 21, 2024
Default avatar

By Safa Mulani

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 us


About the authors
Default avatar
Safa Mulani

author


Default avatar

Technical Editor


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel