How To Find The Size Of A Tuple In Python ?

Python

Nicesnippets

380

21-08-2021


Hi Guys,

In this blog, I will show you how to find the size of a tuple in python. We will talk about python get the size of tuple example. 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

The getsizeof() function belongs to the python’s sys module. It has been implemented in the below example.

In this example I will give you two example for how to get the size of a tuple in python. So let's see the bellow example:

Example 1


import sys

Tuple1 = (1,"abc","xyz",2,3)

Tuple2 = ("a","b","c","d","e","f","g","h")

Tuple3 = ((1, "Orange"), ( 2, "Banana"), (3, "Apple"), (4, "Graphs"))

print("Size of Tuple1: " + str(sys.getsizeof(Tuple1)) + "bytes")

print("Size of Tuple2: " + str(sys.getsizeof(Tuple2)) + "bytes")

print("Size of Tuple3: " + str(sys.getsizeof(Tuple3)) + "bytes")

Example 2

Tuple1 = (1,"abc","xyz",2,3)

Tuple2 = ("a","b","c","d","e","f","g","h")

Tuple3 = ((1, "Orange"), ( 2, "Banana"), (3, "Apple"), (4, "Graphs"))

print("Size of Tuple1: " + str(Tuple1.__sizeof__()) + "bytes")

print("Size of Tuple2: " + str(Tuple2.__sizeof__()) + "bytes")

print("Size of Tuple3: " + str(Tuple3.__sizeof__()) + "bytes")

It will help you....


Recommended Posts