To answer the question in the title: Use len() to get the size of a set.

Semantically that is a bit weird, as a set is an unordered collection, and the length analogy kind of breaks down (we are actually measuring cardinality), but in the spirit of Python using len to get the size of all kinds of collections is easy to remember and it is quite obvious what the statement means.

Answer from leo on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_set_length.asp
Python Get the Length of a Set
Python Examples Python Compiler ... Python Certificate Python Training ... To determine how many items a set has, use the len() method....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ find-the-length-of-a-set-in-python
Find the length of a set in Python - GeeksforGeeks
We are given a set and our task is to find its length. For example, if we have a = {1, 2, 3, 4, 5}, the length of the set should be 5. len() function in Python returns the number of elements in a set.
Published ย  July 12, 2025
Discussions

how to find the size of set in python? - Stack Overflow
Python's set class represents the mathematical notatation of a set. The size of a set means the amount of memory(in bytes) occupied by a set object. ... To answer the question in the title: Use len() to get the size of a set. Semantically that is a bit weird, as a set is an unordered collection, and the length ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I get the number of elements in a list (length of a list) in Python? - Stack Overflow
The argument may be a sequence ... frozen set). len is implemented with __len__, from the data model docs: ... 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__() ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to set a max length for a python list/set? - Stack Overflow
In c/c++, we could have: maxnum = 10; double xlist[maxnum]; How to set a maximum length for a python list/set? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python set not changing the length of new list? - Unix & Linux Stack Exchange
Using python I converted a list to set and back to list to remove the duplicates. But the length of the new list is unmodified. How do we get the length of the new list? Python code: values = [2,... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_sets.asp
Python Sets
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 ... Sets are used to store multiple items in a single variable.
Top answer
1 of 11
2987

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.

๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-get-the-length-of-a-set-in-python
How to get the length of a set in Python
A set is written with braces ({}). ... In the code above, the variable myset represents the set, while "USA", "CANADA", and "BRAZIL" are the elements of the set. To determine the length of a set in Python, we use the len() function.
Find elsewhere
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ sets โ€บ python-sets-exercise-15.php
Python: Find the length of a set - w3resource
July 5, 2025 - Write a Python program to implement a function that returns the size of a set by converting it to a list and counting. Write a Python program to compare the length of two sets and output the difference in their sizes.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
1 week ago - Return centered in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s). For example: >>> 'Python'.center(10) ' Python ' >>> 'Python'.center(10, '-') '--Python--' >>> 'Python'.center(4) 'Python'
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ find the length of the set in python
Find the Length of the Set in Python - Spark By {Examples}
May 31, 2024 - The len() function in Python is used to get the length of the Set. Actually, length gets you the number of elements in the sequence/iterable which
๐ŸŒ
Codingal
codingal.com โ€บ coding-for-kids โ€บ blog โ€บ sets-in-python
Sets in Python | Codingal
February 3, 2025 - When you apply a function to a Python set, it executes operations on the set and returns a value. ... The length of a set is returned by the len() method.
๐ŸŒ
Real Python
realpython.com โ€บ len-python-function
Using the len() Function in Python โ€“ Real Python
November 16, 2024 - The function is efficient because it typically retrieves the length from an attribute stored within the object. How can I use len() with different data types in Python?Show/Hide ยท You can use len() with several built-in data types, including strings, lists, tuples, dictionaries, sets, and range objects.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ python โ€บ python-find-length-of-a-set
How to find length of a Set in Python?
April 3, 2023 - # initialize set aSet = {'apple', 'cherry', 'banana'} # find set length n = len(aSet) # print the length print(f'Length of the set is : {n}') ... In this Python Tutorial, we learned how to find the length of a given Set using len() function with ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ find-the-size-of-a-set-in-python
Find the size of a Set in Python - GeeksforGeeks
September 10, 2025 - A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Pythonโ€™s set class represents the mathematical notion of a set. The size of a set here refers to the memory occupied (in bytes), not the number of elements.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ functions.html
Built-in Functions โ€” Python 3.14.3 documentation
6 days ago - 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).
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ itertools.html
itertools โ€” Functions creating iterators for efficient looping
1 week ago - The output is a subsequence of product() keeping only entries that are subsequences of the iterable. The length of the output is given by math.comb() which computes n! / r! / (n - r)!
๐ŸŒ
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 - Length of 'Python' is 6 characters. Length of 'rรฉsumรฉ' is 6 characters. Tuples are ordered, immutable sequences, much like lists. len() behaves identically, counting the total number of elements in the tuple. Sets are unordered collections of unique elements.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-set-length
Python - Set Length
To get the length of a given Set in Python, you can use len() built-in function. Call len(), and pass the given set as argument. The len() function returns an integer representing the number of items in the Set.