The len() function can be used with several different types in Python - both built-in types and library types. For example:

>>> len([1, 2, 3])
3
Answer from gnud on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_list_length.asp
Python List Length
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
Top answer
1 of 11
2989

The len() function can be used with several different types in Python - both built-in types and library types. For example:

>>> len([1, 2, 3])
3
2 of 11
320

How do I get the length of a list?

To find the number of elements in a list, use the builtin function len:

items = []
items.append("apple")
items.append("orange")
items.append("banana")

And now:

len(items)

returns 3.

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:

items.__len__()

returns 3.

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:

>>> all(hasattr(cls, '__len__') for cls in (str, bytes, tuple, list, 
                                            range, dict, set, frozenset))
True

Do not use len to test for an empty or nonempty list

To test for a specific length, of course, simply test for equality:

if len(items) == required_length:
    ...

But there's a special case for testing for a zero length list or the inverse. In that case, do not test for equality.

Also, do not do:

if len(items): 
    ...

Instead, simply do:

if items:     # Then we have some items, not empty!
    ...

or

if not items: # Then we have an empty list!
    ...

I explain why here but in short, if items or if not items is more readable and performant than other alternatives.

🌐
Tutorialspoint
tutorialspoint.com › python › list_len.htm
Python List len() Method
Following is the syntax for the Python List len() method − · len(list) list − This is a list for which number of elements to be counted. This method returns the number of elements in the list.
🌐
DataCamp
datacamp.com › tutorial › python-list-size
Python List Size: 8 Different Methods for Finding the Length of a List in Python | DataCamp
February 7, 2024 - # Use len() to find the size of the list length = len(my_list) print(length) # Output: 4 · Referred to as the naive method, this approach involves initializing a counter and incrementing it for each element encountered in a for loop through the list. You can learn more about for loops and other types of loops in this tutorial. # Naive method using a for loop to count the list's size counter = 0 for item in my_list: counter += 1 print(counter) # Output: 4 · List comprehensions in Python offer a compact syntax for performing operations on a list.
🌐
4Geeks
4geeks.com › how-to › how-to-get-python-list-length
How to Get the Length of a List in Python?
July 16, 2025 - To get the length of a list in Python, you can use the len() function. Here’s the simplest way to use this function: ... The len() function returns the number of items in the list.
🌐
DigitalOcean
digitalocean.com › community › tutorials › find-the-length-of-a-list-in-python
How to find the length of a list in Python | DigitalOcean
July 25, 2025 - The len() function is universal and can be used to get the length of any object that has a defined length, including other sequence types (like strings, tuples, and ranges) and collection types (like dictionaries and sets). The syntax is straightforward: you pass the list as the sole argument ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-find-length-of-list
How To Find the Length of a List in Python - GeeksforGeeks
May 2, 2025 - Let's explore different methods to find the length of a list with examples. len() function is a built-in Python function and it's the simplest and most efficient way to find the length of a list in Python.
🌐
Carmatec
carmatec.com › home › finding the length of a list in python with examples
Finding the Length of a List in Python With Examples
December 31, 2024 - Understand List Structure: For nested lists, ensure you understand whether you need the top-level length or a total count of all elements. Finding the length of a list in Python is a straightforward task thanks to the len() function.
Find elsewhere
🌐
Cherry Servers
cherryservers.com › home › blog › cloud computing › how to get the length of a list in python
How to Get the Length of a List in Python | Cherry Servers
November 7, 2025 - The most common way to get the Python list length is through the built-in function len(). Len in Python is a function that takes in a single argument and returns the number of elements or items in that argument.
🌐
Edureka
edureka.co › blog › python-list-length
How to Get the Length of List in Python? | Edureka
November 27, 2024 - The len() method is one of the easiest ways to find the length of list in Python. This is the most conventional technique adopted by all programmers.
🌐
iO Flood
ioflood.com › blog › python-length-of-list
Finding the Length of a List in Python (With Examples)
August 21, 2024 - While the len() function is the most straightforward way to find the length of a list in Python, it’s not the only way. In this section, we will introduce two alternative methods: the reduce() function from the functools module and list ...
🌐
freeCodeCamp
freecodecamp.org › news › python-list-length-how-to-get-the-size-of-a-list-in-python
Python List Length – How to Get the Size of a List in Python
March 3, 2022 - 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.
🌐
Python.org
discuss.python.org › ideas
List length operation "len(list)" should include (dot) operator format " list.len() "
October 30, 2021 - list.append() list.remove() list.insert() list.pop() list.slice() and so on but when it comes to list length it’s len(list). We should have the option to write list.len() thus making the language more uniform and orthogonal. and easier to ...
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to find the list length in python
How to Find List Length in Python {3 Methods}
November 27, 2025 - This method is the most straightforward and common way to determine a list length. To use the len() function, provide the list as an argument. For example: ... The built-in len() function returns the element count as an integer.
🌐
ReqBin
reqbin.com › code › python › zbhfimqp › python-list-length-example
How do I get the length of a list in Python?
To get the length of a list in Python, you can use the len(list) function. The len() function works similarly with other data structures, including tuples, sets, and dictionaries.
🌐
MonoVM
monovm.com › 🐍 python 🐍 › determining list length in python | methods & examples
Determining List Length in Python | Methods & Examples
November 7, 2023 - Using `len()` can significantly reduce the amount of code you need to write compared to the Naïve Counter Method. Here's how to use the `len()` function to find the length of a list: Syntax: The `len()` function takes a single argument, which ...
🌐
Cisco
ipcisco.com › home › python list length
Python List Length | How To Use Python Length() Method ⋆
March 19, 2021 - We can use this for string list also. length = len([“Audi”, “BMW”, “Mercedes”, ”Volvo”]) print(length) The output of this python code will be 4.
🌐
Flexiple
flexiple.com › python › python-list-length
Python List Length – How to Get the Size of a List in Python - Flexiple - Flexiple
Whether you're a beginner or an advanced programmer, understanding how to accurately obtain the list length can enhance your code's efficiency and readability. This guide delves into three methods: utilizing the len() function, iterating with ...
🌐
Python Examples
pythonexamples.org › python-list-length
Python List Length - len(list)
Python List Length Example - To get the length of list in Python, you can call the Python builtin function len() with the list passed as argument. len() returns an integer representing the number of elements in the list.
🌐
Oreate AI
oreateai.com › blog › mastering-list-lengths-in-python-a-comprehensive-guide › 588e0244a6d5b3bfeeffc5a4e8ae8143
Mastering List Lengths in Python: A Comprehensive Guide - Oreate AI Blog
January 7, 2026 - The most straightforward way to find out the length of a list is by using Python's built-in len() function. This little gem allows you to quickly retrieve the number of items contained within any iterable object—be it lists, strings, tuples, ...