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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Fill Strings for size K in Tuple List
- Python | Find whether all tuple have same length
- Python | Find number of lists in a tuple
- Python | Find Maximum difference between tuple pairs
- Python - Find minimum k records from tuple list
- Python - Find first element by second in tuple List
- Python - Find frequency of given Datatype in tuple
- PyQt5 - How to adjust size of ComboBox according to the items size
- Python | Count the elements in a list until an element is a Tuple
- Python | Count occurrences of an element in a Tuple
- Python | Sort a tuple by its float element
- Python | Reversing a Tuple
- Python | Pair and combine nested list to tuple list
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.