Find the Size of a Tuple in Python
Last Updated :
29 Oct, 2025
Given a tuple, the task is to find its size, i.e., the number of elements in it. For example:
Input: (10, 20, 30, 40, 50)
Output: 5
Let's explore different methods to find the size of a tuple.
Using len()
len() returns the count of items stored within the tuple.
Python
tup = (0, 1, 2, 'a', 3)
print(len(tup))
Explanation: tup has 5 elements (0, 1, 2, 'a', and 3), so len(tup) returns 5.
Using sys.getsizeof()
sys.getsizeof() from sys module is used to find the memory size of a tuple (in bytes). It gives us the size of the tuple object itself, including the overhead Python uses for its internal structure.
Python
import sys
tup = (0, 1, 2, 'a', 3)
print(sys.getsizeof(tup))
Note: The size may vary depending on the version of Python and the system architecture.
Using memoryview()
memoryview() is typically used for low-level memory management and binary data. It creates a view object that provides access to the memory buffer of an object. While not often used for simple tuples, it can be helpful in performance-sensitive scenarios.
Python
tup = (0, 1, 2,'a', 3)
res = memoryview(bytearray(str(tup),'utf-8'))
print(res.nbytes)
Explanation:
- bytearray(str(tup), 'utf-8') converts tuple into bytes.
- memoryview provides a view into the memory buffer.
- memory_view.nbytes returns the number of bytes used by this byte representation.
Using id()
id() function to obtain memory address of the tuple or its elements. This method helps us to understand where objects are stored in memory, but it doesn’t directly give us their size.
Python
tup = (0, 1, 2, 'a', 3)
for item in tup:
print(f"Memory address of {item}: {id(item)}")
OutputMemory address of 0: 140279631445936
Memory address of 1: 140279631445968
Memory address of 2: 140279631446000
Memory address of a: 140279631510488
Memory address of 3: 140279631446032
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice