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
๐ŸŒ
4Geeks
4geeks.com โ€บ how-to โ€บ how-to-get-python-list-length
How to Get the Length of a List in Python?
July 16, 2025 - The len() function returns the number of items in the list. In this case, list_length will be 5. The len() function is not only straightforward but also highly efficient, making it ideal for any list size.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ length of a list in python
Length of a List in Python - AskPython
January 29, 2023 - Then we directly pass the list into the len() method and it returns the length of the list. This in our case is 6. Now let us define our own function in Python which will calculate the length of a list passed to it and return it where the function ...
Discussions

How do I get the number of elements in a list (length of a list) in Python? - Stack Overflow
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. ... Return the length ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
length of Python list when list has a single value - Stack Overflow
Okay I concede that I didn't ask the question very well. I will update my question to be more precise. I am writing a function that takes a list as an argument. I want to check the length of the l... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python list length
 len(<========8>~)
More on reddit.com
๐ŸŒ r/ProgrammerHumor
37
577
January 19, 2018
How to determine the size of all combined elements in a list of lists
This will loop through everything once, but it'll handle any depth (up to recursion limits): from collections.abc import Sequence def len_items(items): """Return the length of all nested iterables.""" return sum( len_items(item) if isinstance(item, Sequence) else 1 for item in items ) More on reddit.com
๐ŸŒ r/learnpython
26
70
December 31, 2020
People also ask

Q1. Which method provides the fastest way to get the length of a list in Python?
The len() function is the fastest and most efficient way in terms of Time &amp; Space Complexity.
๐ŸŒ
intellipaat.com
intellipaat.com โ€บ home โ€บ blog โ€บ how to find length of list in python
Python List Length - How to Find the Length of a List in Python
Q6. How to get the total number of elements in a list of lists in Python?
Use the sum() function along with a generator expression that goes through the sublists to count their lengths.
๐ŸŒ
intellipaat.com
intellipaat.com โ€บ home โ€บ blog โ€บ how to find length of list in python
Python List Length - How to Find the Length of a List in Python
Q3. Does len() work on other data structures like tuples and sets?
Yes, len() works with tuples, sets, dictionaries, and other iterable objects.
๐ŸŒ
intellipaat.com
intellipaat.com โ€บ home โ€บ blog โ€บ how to find length of list in python
Python List Length - How to Find the Length of a List in Python
๐ŸŒ
Bradcypert
bradcypert.com โ€บ python-length-of-a-list
Python Length of a List - BradCypert.com
December 27, 2022 - Finding the length of a list in Python is easy via the len() function. Python's len() function also works on collections and all sequences!
Top answer
1 of 11
2991

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
323

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.

๐ŸŒ
Quora
quora.com โ€บ How-do-you-check-the-length-of-a-list-in-Python
How to check the length of a list in Python - Quora
We will look at two simple methods to find the lengths of a list. ... First we will look at a very basic method that anyone with a little knowledge of the Python language will be able to grasp. We will use a loop and a counter in this technique.
Find elsewhere
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ python-list-length
Python List Length
August 12, 2021 - The len() function (which is short ... the number of items in the given object. To find a Python list's length, you would then use len() as follows:...
Top answer
1 of 5
5

The proper python syntax for a list consisting of a single item is [ 'ed' ].

What you're doing with list('ed') is asking python to convert 'ed' to a list. This is a consistent metaphor in python: when you want to convert something to a string, you say str(some_thing). Any hack you'd use to make list('ed') return a list with just the string 'ed' would break python's internal metaphors.

When python sees list(x), it will try to convert x to a list. If x is iterable, it does something more or less equivalent to this:

def make_list(x):
  ret_val = []
  for item in x:
    ret_val.append(item)
  return ret_val

Because your string 'ed' is iterable, python will convert it to a list of length two: [ 'e', 'd' ].

The cleanest idiomatic python in this case might be to have your function accept a variable number of arguments, so instead of this

def my_func(itemList):
  ...

you'd do this

def my_func(*items):
  ...

And instead of calling it like this

my_func(['ed','lu','lsky'])

You'd call it like this:

my_func('ed', 'lu', 'lsky')

In this way you can accept any number of arguments, and your API will be nice and clean.

2 of 5
3

You can ask if your variable is a list:

def my_method(my_var):
    if isinstance(my_var, list):
        for my_elem in my_var:
            # do stuff with my_elem
    else:  # my_var is not iterable
        # do stuff with my_var

EDIT: Another option is to try iterating over it, and if it fails (raises and exception) you assume is a single element:

def my_method(my_var):
    try:
        for my_elem in my_var:
            # do stuff with my_elem
    except TypeError:  # my_var is not iterable
        # do_stuff with my_var

The good thing about this second options is that it will work not only for lists, as the first one, but with anything that is iterable (strings, sets, dicts, etc.)

๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Get the Size (Length, Number of Items) of a List in Python | note.nkmk.me
August 24, 2023 - In Python, you can get the size (length, number of items) of a list using the built-in len() function. Built-in Functions - len() โ€” Python 3.11.3 documentation Get the size of a list with len() Get t ...
๐ŸŒ
Tech Edu Byte
techedubyte.com โ€บ home โ€บ python list length: how to use the len() function
Python List Length: How to Use the len() Function - Tech Edu Byte
February 8, 2026 - The len() function is a built-in Python function that returns the number of items in a sequence or collection, such as a list, tuple, string, or dictionary. For lists, it simply counts the number of elements and returns that count as an integer.
๐ŸŒ
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 most direct, efficient, and โ€œPythonicโ€ way to get the number of items in a list is by using Pythonโ€™s built-in len() function. This function is a core part of the language and is designed to be highly optimized for this exact purpose. The len() function is universal and can be used ...
๐ŸŒ
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 - The length of a Python list is simply the number of items it contains. For example, a list [1, 2, 3] has a length of 3.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_list_length.asp
Python List Length
Python DSA Lists and Arrays Stacks Queues Linked Lists Hash Tables Trees Binary Trees Binary Search Trees AVL Trees Graphs Linear Search Binary Search Bubble Sort Selection Sort Insertion Sort Quick Sort Counting Sort Radix Sort Merge Sort
๐ŸŒ
HubPages
discover.hubpages.com โ€บ technology โ€บ Python-Find-Length-of-a-List
Python: Find Length of a List With "Len()" - HubPages
March 27, 2025 - Learning Python? You'll need to know how to find the length of a list. Use the built-in function len() to find the length or size of a list, which is Python's version of an array. Read on for my full explanation.
๐ŸŒ
Cisco
ipcisco.com โ€บ home โ€บ python list length
Python List Length | How To Use Python Length() Method โ‹†
March 19, 2021 - Here, we will use the belowm python code: list = [15, "cats", 63, "dogs"] length = len(list) print(length) As a return fom this code, we will receive 4. Because there are 4 members of this mixed list.
๐ŸŒ
MonoVM
monovm.com โ€บ ๐Ÿ python ๐Ÿ โ€บ determining list length in python | methods & examples
Determining List Length in Python | Methods & Examples
November 7, 2023 - Finish the Loop: After the loop has iterated through all the elements, the counter will hold the length of the list. Let's take a look at a Python code example for a clear understanding:
๐ŸŒ
Hackr
hackr.io โ€บ home โ€บ articles โ€บ programming
9 Ways To Find Python List Length [2026] | Beginner to Pro
January 30, 2025 - So to recap, the Python list is a versatile and widely used data structure for any type of Python project that allows us to create an ordered collection of mutable and dynamic elements. And when it comes to the length of a list in Python, this is simply the number of elements within the list.
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ how to find length of list in python
Python List Length - How to Find the Length of a List in Python
May 29, 2025 - Explore various methods to find Python list length using len() function manual counting list comprehension custom function and naive counter. Read more