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
🌐
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.
Discussions

List length operation "len(list)" should include (dot) operator format " list.len() "
Adding yet one way of getting the length of the list would make the language less orthogonal · Why do you need yet one way of getting length which will only work with lists if there is a standard way which works in all Python versions and with all containers · Hi Andrew, in other languages ... More on discuss.python.org
🌐 discuss.python.org
1
October 30, 2021
Why no .get(idx[, default]) on python list??
There was a lengthy thread on this on Python-Ideas a couple of years ago: https://mail.python.org/archives/list/python-ideas@python.org/thread/LLK3EQ3QWNDB54SEBKJ4XEV4LXP5HVJS/ The clearest explanation of the most common objection was from Marc-Andre Lemburg: dict.get() was added since the lookup is expensive and you want to avoid having to do this twice in the common case where the element does exist. It was not added as a way to hide away an exception, but instead to bypass having to generate this exception in the first place. dict.setdefault() has a similar motivation. list.get() merely safes you a line of code (or perhaps a few more depending on how you format things), hiding away an exception in case the requested index does not exist. If that's all you want, you're better off writing a helper which hides the exception for you. I argue that making it explicit that you're expecting two (or more) different list lengths in your code results in more intuitive and maintainable code, rather than catching IndexErrors (regardless of whether you hide them in a method, a helper, or handle them directly). So this is more than just style, it's about clarity of intent. More on reddit.com
🌐 r/Python
94
144
June 20, 2022
List of all Python dunder methods?

Sounds like a classic case of X-Y-problem. What are you trying to achieve?

I don't think trying to maintain a comprehensive list of dunder methods is a good idea. Consider that this list is bound to change with upcoming versions of Python and has already undergone considerable changes in the past. You may end up maintaining varying lists of magic methods for different versions of Python.

I think the goto approach here would be to just try to call the respective method from within a generic __getattribute__ and react appropriately to TypeError.

It may also be possible to analyse the proxied object's class dict (someobject.__class__.__dict__) to find out which dunder methods were defined on that class. Of course you may end up having to walk up the inheritance chain as well, so this is bound to get quite complicated.

More on reddit.com
🌐 r/Python
15
7
February 20, 2018
Can VSCode outline class members for Python file?
It's a bug in the Python extension. It works correctly if you opt out of using the new language server. More on reddit.com
🌐 r/vscode
5
2
June 24, 2018
Top answer
1 of 11
2990

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
321

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.

🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
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 ...
🌐
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.
🌐
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 ...
🌐
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 ...
🌐
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.
🌐
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.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › how to find length of list in python
How to Find Length of List in Python
June 27, 2025 - Python lists store their size internally, so len() just retrieves this stored value. Readability: len(my_list) is clear and easy to understand. Simplicity: It is a single function call. ... The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods.
🌐
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, ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › find the length of a list in python
Find the Length of a List in Python - Spark By {Examples}
May 31, 2024 - You can use the built-in len() function in python to get the length of the list which is a number of elements in a list. you can also use this function with tuple, dictionary, string, or any other collection.