Python - Create an Empty Array


Create an Empty Array in Python

In Python, you can create an empty array using array() method of array module.

In this tutorial, you will learn how to create an empty Python array, with examples.

To create an empty array, import array module, call array() method of array module, and pass the type code for the required type of elements in the array as argument. The array() method returns an empty Python array of specified type.

1. Create an empty integer array

In the following program, we create an empty integer array my_array using array() method. Pass 'i' type code as argument to the array() method.

Python Program

import array as arr

my_array = arr.array('i')

print(my_array)

Output

array('i')

2. Create an empty float array

In the following program, we create an empty floating point number array my_array using array() method. Pass 'f' type code as argument to the array() method.

Python Program

import array as arr

my_array = arr.array('f')

print(my_array)

Output

array('f')

3. Create an empty character array

In the following program, we create an empty character (unicode character) array my_array using array() method. Pass 'u' type code as argument to the array() method.

Python Program

import array as arr

my_array = arr.array('u')

print(my_array)

Output

array('u')

Summary

In this Python Array tutorial, we learned how to create an empty array of specific datatype, using array() method of array module, with examples.