Use len() to find length of a python list banner

Get Length of a List in Python

In python, you can determine the length of a list using the in-built len() function. Lists provide a convenient way of storing elements. They store a sequential collection of elements and can have duplicate values as well. In this tutorial, we’ll look at the different ways in which you can find the length of a list.

Before we proceed, here’s a quick refresher on python lists – Lists are used to store an ordered collection of items. These items can be any type of object from numbers to strings or even another list. This makes lists are one of the most versatile data structures in python to store a collection of objects. For more, check out our guide on lists and other data structures in python.

The most simple way to find the length of a list in python is to use the in-built function len(). But there are other ways as well. Here, we show with examples some of the different ways you can use to find the length of a list.

As stated above, len() is an in-built function in python which returns the length of an object. It not only works with lists but can also be used to determine the length of other iterables like set, tuple, dictionary, string, etc.

Syntax:

len(object)

Here, object is the object whose length is to be determined. It returns the length as an integer.

Example: Length of a list using len()

# determine the lenth of the list using len()
vowels = ['a', 'e', 'i', 'o', 'u']
print("Length of vowels:", len(vowels))

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Length of vowels: 5

In the above example, the len() function is used to determine the length of the list vowels which comes out to be 5.

Behind the scenes, the len() function is implemented using the __len()__ function associated with the object.

# determine the lenth of the list using len()
vowels = ['a', 'e', 'i', 'o', 'u']
print("Length of vowels:", len(vowels))
print("Length of vowels:", vowels.__len__())

Output:

Length of vowels: 5
Length of vowels: 5

In the above example, we see we get the same results from len() and __len__(). This is because under the hood the len() function actually implements __len__()

You can determine the length of a list by setting up a counter and incrementing it by one as you traverse through the entire list. This is called the naive method.

# determine the lenth of the list using len()
vowels = ['a', 'e', 'i', 'o', 'u']
# counter
c = 0
# iterate through the list
for v in vowels:
    c += 1

print("Length of vowels:", c)

Output:

Length of vowels: 5

In the above example, the counter c is set to 0 and is incremented by 1 each time as we traverse through the loop.

You should prefer the len() function to determine the length of a list rather than iterating through the list every time you require to determine its length because –

  1. Simplicity. Using len() is quite simple as against using a loop. Not only does it reduce the code to fewer lines, but it’s also very readable.
  2. The len() function is O(1) in time complexity whereas using a loop is O(n). This means that the len() function is very quick in determining the length of a list. 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 you use the len() function, it basically “looks up” for the length attribute of the object and returns it.
    This answer on Stack Overflow explains this.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top