Signup/Sign In

Python program to Find the size of a Tuple

In this tutorial, we will learn to write a Python program that will find the size of a tuple. Let us see what a tuple is, tuples are generally a data structure for storing one or more values. The values in a tuple can be of different data types. The values are accessed using integer indexes. Tuples are enclosed within parenthesis ().

The size of a tuple means the amount of space taken by the tuple in memory. We will see different methods for finding the size of a tuple in Python.

Look at the examples to understand the input and output format.

Input:

tup1= ('a', 'b', 'c', 'd')
tup2= ("Python", "is", "helpful")
tup3= ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12))

Output:

36 bytes
32 bytes
36 bytes

To find the size of tuples in Python, we can use the following approaches:

  1. Using a getsizeof()method
  2. Using__sizeof__()method

Approach 1: using getsizeof() Method

The getsizeof() method belongs to the sys module in Python. It returns the size of an object in bytes. The function will work for all the built-in objects in Python.

To use this method we will have to import sys module in our program.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Import sys module

Step 2- Initialise tuple values

Step 3- Print size using the getsizeof() method

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. The getsizeof() method will return the total space occupied in memory by the tuple, which also includes space taken by garbage values.

import sys
tup1= ("Studytonight", 1, 2, 3)
tup2= ("Python", "Java", "C++")
tup3= ((1, 2), (4, 6), (7, 2), (10, 9))

print("Size of tuple1: ", sys.getsizeof(tup1), "bytes")
print("Size of tuple2: ", sys.getsizeof(tup2), "bytes")
print("Size of tuple3: ", sys.getsizeof(tup3), "bytes")


Size of tuple1: 36 bytes
Size of tuple2: 32 bytes
Size of tuple3: 36 bytes

Approach 2: using __sizeof__() Method

In this approach, we will use the built-in method for finding the size of an object. This method will return the size in bytes taken up by the object. here the object will be a tuple.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise multiple tuple values

Step 2- Use the __sizeof__() method

Step 3- Print the size in bytes

Python Program 2

The __sizeof__() method returns the size without considering the size of the additional garbage value.

tup1= ("Study","tonight", 1, 3)
tup2= ("Python", "Language")
tup3= ((1,"Java"), (2, "Python"), (3, "C"))

#print size
print("Size of tuple1: ", tup1.__sizeof__(), "bytes")
print("Size of tuple2: ", tup2.__sizeof__(), "bytes")
print("Size of tuple3: ", tup3.__sizeof__(), "bytes")


Size of tuple1: 28 bytes
Size of tuple2: 20 bytes
Size of tuple3: 24 bytes

Conclusion

We have discussed two different methods for finding the size of a tuple in Python in bytes. In the first approach, we have used a method of the sys module. In the second approach, we have used a built-in method of tuple for finding the size of an object.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.