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.
How to find the length of a list 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.
Using the len() function
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.
Here, object is the object whose length is to be determined. It returns the length as an integer.
Example: Length of a list using
# determine the lenth of the list using len() vowels = ['a', 'e', 'i', 'o', 'u'] print("Length of vowels:", len(vowels))
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
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__())
Length of vowels: 5 Length of vowels: 5
In the above example, we see we get the same results from
__len__(). This is because under the hood the
len() function actually implements
By traversing through the list (Naive method)
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)
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.
Why should you prefer len()?
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 –
- 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.
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.
Tutorials on python lists:
- Python – Check if an element is in a list
- Python – Iterate over multiple lists in parallel using zip()
- Python – Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python – Convert List to a String
- Convert Numpy array to a List – With Examples
- Python List Comprehension – With Examples
- Python List Index – With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list – With Examples
- Python Reverse a List – With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.