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.
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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__()
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)
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.
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. - The
len()
function isO(1)
in time complexity whereas using a loop isO(n)
. This means that thelen()
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 thelen()
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.