Python Array - Item/Element Size in Bytes
Size of an Element in Bytes in Python Array
In Python, you can find the size of an element in given array in number of bytes using itemsize attribute of the array object.
In this tutorial, you will learn how to find the size of an element in the given Python array, with examples.
To find the size of an element in number of bytes in the given array my_array in Python, read the itemsize attribute of the array as shown in the following.
my_array.itemsize
The itemsize attribute returns an integer value representing the number of bytes an element in the given array occupies in the memory.
1. Size of an Element in Integer Array
In the following program, we take an integer array my_array, and then use itemsize attribute to find the number of bytes each element in the array occupy in memory.
Python Program
import array as arr
my_array = arr.array('i', [25, 100, 150])
my_array_itemsize = my_array.itemsize
print("Item size :", my_array_itemsize)
Output
Item size : 4
Each element of the given array occupies a size 4 bytes in memory.
2. Size of an Element in Double Array
In the following program, we take a double type array my_array, and then use itemsize attribute to find the size of each element in the array.
Python Program
import array as arr
my_array = arr.array('d', [5.14, 0.25, 3.78])
my_array_itemsize = my_array.itemsize
print("Item size :", my_array_itemsize)
Output
Item size : 8
Summary
In this Python Array tutorial, we learned how to find the size of each element in bytes in the given Python array, using itemsize attribute, with examples.