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
- Find the size of a Tuple in Python
- How to find size of an object in Python?
- Find the size of a list - Python
- Find size of a list in Python
- Find the size of a Dictionary in Python
- Python Counter to find the size of largest subset of anagram words
- Python | Pandas TimedeltaIndex.size
- SymPy | Polyhedron.size() in Python
- SymPy | Subset.size() in Python
- SymPy | Permutation.size() in Python
- Python | Pandas Panel.size
- SymPy | Prufer.size() in Python
- Numpy size() function | Python
- Python program to get all subsets of given size of a set
- Python | Pandas Series.size
- Python | os.path.size() method
- Python | Pandas Index.size
- Python | Size of string in memory
- Python - All Possible unique K size combinations till N
- Python - Filter above Threshold size Strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.