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.
how to find the size of set in python? - Stack Overflow
How do I get the number of elements in a list (length of a list) in Python? - Stack Overflow
How to set a max length for a python list/set? - Stack Overflow
Python set not changing the length of new list? - Unix & Linux Stack Exchange
Videos
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
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.
You don't and do not need to.
Python lists grow and shrink dynamically as needed to fit their contents. Sets are implemented as a hash table, and like Python dictionaries grow and shrink dynamically as needed to fit their contents.
Perhaps you were looking for collections.deque (which takes a maxlen parameter) or something using a heapq (using heapq.heappushpop() when you have reached the maximum) instead?
Once you have your list, lst, you can
if len(lst)>10:
lst = lst[:10]
If size more than 10 elements, you truncate to first ten elements.