Python – Find the Type of Array

Find the Type of Array in Python

In Python, you can find the type of given array using typecode attribute of the array object.

In this tutorial, you will learn how to find the type of given Python array, with examples.

To find the type of the given array my_array in Python, read the typecode attribute of the array as shown in the following.

my_array.typecode

The typecode attribute returns a string value representing the type of the elements in the given array. The typecode returned would be one of the type code shown in the following table.

Type
Code
C
data type
Python
data type
'b'signed charint
'B'unsigned charint
'u'Py_UNICODEUnicode
'h'signed shortint
'H'unsigned shortint
'i'signed intint
'I'unsigned intint
'l'signed longint
'L'unsigned longint
'f'floatfloat
'd'doublefloat

1. Get Type of given Integer Array

In the following program, we take an integer array my_array, and then use typecode attribute to programmatically find the type of elements in the given array.

Python Program

import array as arr

my_array = arr.array('i', [100, 200, 300, 400])
my_array_typecode = my_array.typecode

print("Array type :", my_array_typecode)
Run Code Copy

Output

Array type : i

From the above table, typecode of 'i' represents signed int.

2. Get Type of given Character Array

In the following program, we take a unicode character array my_array, and then use typecode attribute to programmatically find the type of elements in the given array.

Python Program

import array as arr

my_array = arr.array('u', 'apple')
my_array_typecode = my_array.typecode

print("Array type :", my_array_typecode)
Run Code Copy

Output

Array type : u

Summary

In this Python Array tutorial, we learned how to find the type of given Python array, using typecode attribute, with examples.

Related Tutorials

Code copied to clipboard successfully 👍