Find the Size of a Tuple in Python (original) (raw)

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:

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)}")

`

Output

Memory address of 0: 140279631445936 Memory address of 1: 140279631445968 Memory address of 2: 140279631446000 Memory address of a: 140279631510488 Memory address of 3: 140279631446032