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
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
322

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.

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.)

Discussions

Does len() a linear count?
Metadata: https://blog.finxter.com/python-list-length-whats-the-runtime-complexity-of-len/ (I just googled it) More on reddit.com
๐ŸŒ r/Python
36
27
June 15, 2022
Why len(list) has a time-complexity of O(1)?
I mean the implementation keeps a variable that holds the current length of the list. So a call to len would just return the current value of the variable, making it O(1). When the list is changed, elements added or removed, the variable holding the current length is also updated accordingly. More on reddit.com
๐ŸŒ r/leetcode
38
41
July 13, 2023
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
[Python] How to find the length of elements in a list?
Try if len(element) == 2: len returns the number of elements in a list, when applied to a string it returns the number of characters. More on reddit.com
๐ŸŒ r/learnprogramming
2
0
November 23, 2016
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 & 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
๐ŸŒ
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 ...
๐ŸŒ
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
Find elsewhere
๐ŸŒ
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 - The length of a list refers to the number of elements in the list. There are several methods to determine the length of a list in Python. For example, consider a list l = [1, 2, 3, 4, 5], length of this list is 5 as it contains 5 elements in it.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
1 month ago - In the table s is an instance of a mutable sequence type, t is any iterable object and x is an arbitrary object that meets any type and value restrictions imposed by s (for example, bytearray only accepts integers that meet the value restriction 0 <= x <= 255). ... If k is not equal to 1, t must have the same length as the slice it is replacing.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists.asp
Python Lists
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
๐ŸŒ
Codefinity
codefinity.com โ€บ courses โ€บ v2 โ€บ 102a5c09-d0fd-4d74-b116-a7f25cb8d9fe โ€บ 39cc7383-2374-4f3f-b322-2cb0109e6427 โ€บ fe628b40-c2e6-44d8-8d58-49dfba369282
Learn Python List Length: Measuring and Managing List Size | Mastering Python Lists
Python provides the len() function, which returns the total number of items in a list. ... A nested list is considered a single item. The len() function doesn't count the individual items inside a nested list as separate items of the main list.
๐ŸŒ
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.
๐ŸŒ
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:
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ how to find the length of list in python [with examples]
How to Find the Length of List in Python [With Examples]
September 14, 2021 - The len() is a built-in function in python to determine the length of a list in python. Explore this tutorial to learn all about it with examples. Start now!
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
Devzery
devzery.com โ€บ post โ€บ the-length-of-a-list-in-python
Guide to Finding the Length of a List in Python
August 9, 2024 - The built-in len() function is the most straightforward and efficient way to determine the length of a list. For more complex scenarios, such as counting elements in nested lists, custom functions, recursion, and other Python features like reduce() ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_list_count.asp
Python List count() Method
Remove List Duplicates Reverse ... Python Bootcamp Python Certificate Python Training ... The count() method returns the number of elements with the specified value....
๐ŸŒ
Statistics Globe
statisticsglobe.com โ€บ home โ€บ python programming language for statistics & data science โ€บ find length of list in python (3 examples)
How to Find Length of List in Python | Get Number of Elements
May 4, 2023 - The len() function in the above example simply counts the number of elements in the list and returns it. Using this function is a very easy and quick way to get the length of a list in Python.
๐ŸŒ
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
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-length-of-list
Finding the Length of a List in Python (With Examples)
August 21, 2024 - Then, we used the len() function, which returned the number of elements in the list, which is 5. The len() function is a straightforward and efficient way to find the length of a list in Python.
๐ŸŒ
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.