Find the size of a Tuple in Python
Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses.
The size of a Tuple means the amount of memory (in bytes) taken by a Tuple object. In this article, we will learn various ways to get the size of a python Tuple.
getsizeof() function belongs to the python’s sys module. It has been implemented in the below example.
Size of Tuple1: 96bytes Size of Tuple2: 96bytes Size of Tuple3: 80bytes
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 Tuple1: 72bytes Size of Tuple2: 72bytes Size of Tuple3: 56bytes