Find the size of a Set in Python
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 means the amount of memory (in bytes) occupied by a set object. In this article, we will learn various ways to get the size of a python set.
getsizeof() function belongs to the python’s sys module. It has been implemented in the below example.
Size of Set1: 736bytes Size of Set2: 736bytes Size of Set3: 224bytes
sys.getsizeof() function includes the marginal space usage, which includes the garbage collection overhead for the object. Meaning it returns the total space occupied by the object in addition to the garbage collection overhead for the spaces being used.
Python also has an inbuilt __sizeof__() method to determine the space allocation of an object without any additional garbage value. It has been implemented in the below example.
Size of Set1: 712bytes Size of Set2: 712bytes Size of Set3: 200bytes