Python List Length – How to Get the Size of a List in Python

Kolade Chris

In Python, you use a list to store various types of data such as strings and numbers.
A list is identifiable by the square brackets that surround it, and individual values are separated by a comma.
To get the length of a list in Python, you can use the built-in len() function.
Apart from the len() function, you can also use a for loop and the length_hint() function to get the length of a list.
In this article, I will show you how to get the length of a list in 3 different ways.
How to Get the Length of a List in Python with a For Loop
You can use the native for loop of Python to get the length of a list because just like a tuple and dictionary, a list is iterable.
This method is commonly called the naïve method.
The example below shows you how to use the naïve method to get the length of a list in Python
How to Get the Length of a List with the len() Function
Using the len() function is the most common way to get the length of an iterable.
This is more straightforward than using a for loop.
The syntax for using the len() method is len(listName) .
The code snippet below shows how to use the len() function to get the length of a list:
How to Get the Length of a List with the length_hint() Function
The length_hint() method is a less known way of getting the length of a list and other iterables.
length_hint() is defined in the operator module, so you need to import it from there before you can use it.
The syntax for using the length_hint() method is length_hint(listName) .
The example below shows you how to use the length_hint() method to get the length of a list:
Final Thoughts
This article showed you how to get the size of a list with 3 different methods: a for loop, the len() function, and the length_hint() function from the operator module.
You might be wondering which to use between these 3 methods.
I would advise that you use len() because you don’t need to do much to use it compared to for loop and length_hint() .
In addition, len() seems to be faster than both the for loop and length_hint() .
If you find this article helpful, share it so it can reach others who need it.
Python : list length
To find the length of a list in Python, use the len() method.
The len() is a built-in Python method that returns the length of a list.
The len() method can be used with a list, tuple, arrays, dictionary, etc. A list data type is used to store the sequence of various types of data. A list is defined as a collection of values or elements of different types. The elements in the list are separated with a comma(,) and surrounded by the square brackets [ ].
The len() method is one of the most used and convenient ways to find the length of a list in Python language.
Syntax
Argument
The len() function takes a list parameter. It returns the number of elements in the list.
The len() method works in O(1) time as a list is an object with a member to store its size.
Example
Output
Python length of a list using for loop
To find the length of a list in Python using for loop, define and initialize a counter variable count to 0 and increment count variable to get each element in the list, and print the count value using the print() function.
Output
Length of list in Python using — length_hint()
The length_hint() is a built-in operator module’s method that returns an estimate of the number of elements in the given object. To work with this function import operator library.
The return value of the method will be exact if the given object supports the len() method. The operator.length_hint() method also works on a string or integer and returns its length.
Output
Python array length
Python does not have a built-in array data type, so to work with an array, import the array module and then create an array using the array() method with some elements.
To find an array length in Python, use the len() method.
How do I get the number of elements in a list (length of a list) in Python?
How do I get the number of elements in the list items ?
11 Answers 11
The len() function can be used with several different types in Python — both built-in types and library types. For example:
How do I get the length of a list?
To find the number of elements in a list, use the builtin function len :
Explanation
Everything in Python is an object, including lists. All objects have a header of some sort in the C implementation.
Lists and other similar builtin objects with a "size" in Python, in particular, have an attribute called ob_size , where the number of elements in the object is cached. So checking the number of objects in a list is very fast.
But if you’re checking if list size is zero or not, don’t use len — instead, put the list in a boolean context — it is treated as False if empty, and True if non-empty.
From the docs
len(s)
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
len is implemented with __len__ , from the data model docs:
object.__len__(self)
Called to implement the built-in function len() . Should return the length of the object, an integer >= 0. Also, an object that doesn’t define a __nonzero__() [in Python 2 or __bool__() in Python 3] method and whose __len__() method returns zero is considered to be false in a Boolean context.
And we can also see that __len__ is a method of lists:
Builtin types you can get the len (length) of
And in fact we see we can get this information for all of the described types:
Do not use len to test for an empty or nonempty list
To test for a specific length, of course, simply test for equality:
But there’s a special case for testing for a zero length list or the inverse. In that case, do not test for equality.
Instead, simply do:
I explain why here but in short, if items or if not items is more readable and performant than other alternatives.
![]()
While this may not be useful due to the fact that it’d make a lot more sense as being «out of the box» functionality, a fairly simple hack would be to build a class with a length property:
You can use it like so:
Essentially, it’s exactly identical to a list object, with the added benefit of having an OOP-friendly length property.
As always, your mileage may vary.
Besides len you can also use operator.length_hint (requires Python 3.4+). For a normal list both are equivalent, but length_hint makes it possible to get the length of a list-iterator, which could be useful in certain circumstances:
But length_hint is by definition only a «hint», so most of the time len is better.
I’ve seen several answers suggesting accessing __len__ . This is all right when dealing with built-in classes like list , but it could lead to problems with custom classes, because len (and length_hint ) implement some safety checks. For example, both do not allow negative lengths or lengths that exceed a certain value (the sys.maxsize value). So it’s always safer to use the len function instead of the __len__ method!
![]()
And for completeness (primarily educational), it is possible without using the len() function. I would not condone this as a good option DO NOT PROGRAM LIKE THIS IN PYTHON, but it serves a purpose for learning algorithms.
(The list object must be iterable, implied by the for..in stanza.)
The lesson here for new programmers is: You can’t get the number of items in a list without counting them at some point. The question becomes: when is a good time to count them? For example, high-performance code like the connect system call for sockets (written in C) connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); , does not calculate the length of elements (giving that responsibility to the calling code). Notice that the length of the address is passed along to save the step of counting the length first? Another option: computationally, it might make sense to keep track of the number of items as you add them within the object that you pass. Mind that this takes up more space in memory. See Naftuli Kay‘s answer.
Example of keeping track of the length to improve performance while taking up more space in memory. Note that I never use the len() function because the length is tracked:
Python List Length or Size: 5 Ways to Get Length of List

In this tutorial, you’ll learn how to use Python to get the length of a list (or, rather, its size). Knowing how to work with lists is an important skill for anyone using Python. Being able to get a Python list length, is particularly helpful. You’ll learn how to get the Python list length using both the built-in len() function, a naive implementation for-loop method, how to check if a list is empty, and how to check the length of lists of lists.
The Quick Answer: Use len() to get the Python list length

Table of Contents
Python List Length using the len() function
The easiest (and most Pythonic) way to use Python to get the length or size of a list is to use the built-in len() function. The function takes an iterable object as its only parameter and returns its length.
Let’s see how simple this can really be:
We can see here that by using the len() function, we can easily return the length of a Python list.
In the next section you’ll learn how to use a for-loop naive implementation to get the size of a Python list.
Python List Length using a for-loop
While this approach is not recommended (definitely use the method above!), this for-loop method does allow us to understand an algorithm in terms of counting items in an iterable.
In order to do this, we iterate over each item in the list and add to a counter. Let’s see how we can accomplish this in Python:
This approach certainly isn’t as straightforward as using the built-in len() function, but it does explain some algorithmic thinking around counting items in Python.
In the next section, you’ll learn how to easily check if a Python list is empty or not empty.
Want to learn more about Python for-loops? Check out my in-depth tutorial on Python for loops to learn all you need to know!
Check if a Python list is empty or not empty
In your programming journey, you’ll often encounter situations where you need to determine if a Python list is empty or not empty.
Now that you know how to get the length of a Python list, you can simply evaluate whether or not the length of a list is equal to zero or not:
But Python makes it actually much easier to check whether a list is empty or not. Because the value of 0 actually evaluates to False , and any other value evaluates to True , we can simply write the following:
This is much cleaner in terms of writing and reading your code.
In the next section, you’ll learn how to get the length of Python lists of lists.
Learn to split a Python list into different-sized chunks, including how to turn them into sublists of their own, using this easy-to-follow tutorial.
Get Length of Python List of Lists
Working with Python lists of lists makes getting their length a little more complicated.
To better explain this, let’s take a look at an immediate example:
We can see here, that the code (correctly) returns 3 . But what if we wanted to get the length of the all the items contained in the outer list?
In order to accomplish this, we can sum up the lengths of each individual list by way of using a Python list comprehension and the sum function.
Let’s see how this can be done:
Finally, let’s see how we can use Python to get the length of each list in a list of lists.
Want to learn more about Python list comprehensions? This in-depth tutorial will teach you all you need to know. More of a visual learner? A follow-along video is also included!
Get Length of Each List in a Python List of Lists
In the example above, you learned how to get the length of all the items in a Python list of lists. In this section, you’ll learn how to return a list that contains the lengths of each sublist in a list of lists.
We can do this, again, by way of a Python list comprehension. What we’ll do, is iterate over each sublist and determine its length. A keen eye will notice it’s the same method we applied above, simply without the sum() function as a prefix.
Let’s take a look at how to do this:
In this section, you learned how to use Python to get the length of each sublist in a list of lists.
Want to learn how to append items to a list? This tutorial will teach your four different ways to add items to your Python lists.
Conclusion
In this post, you learned how to use Python to calculate the length of a list. You also learned how to check if a Python list is empty or not. Finally, you learned how to calculate the length of a list of lists, as well as how to calculate the length of each list contained within a list of lists.
To learn more about the Python len() function, check out the official documentation here.