append function of array class

The append() method adds an element past the last element of an existing array object. The example Python program adds an element to the end of a floating-point array.

Array Updated 11 Jan 2020 1 min read

Overview

  • The append() method of the array class adds an element past all the existing elements of an array instance.
  • If the new element added is different from the base type of the array Python raises a TypeError.

Example

python
# Example Python program that appends an element to

# an array of floating-point numbers

import array

# Create an array of floating point numbers

floatArray = array.array("f", [3.14, 6.28, 12.56]);

print("Array of floating point numbers:");

print(floatArray);

# Append an element to the array

floatArray.append(25.12);

print("Array after appending an element:");

print(floatArray);

Output

Output
Array of floating point numbers:

array('f', [3.140000104904175, 6.28000020980835, 12.5600004196167])

Array after appending an element:

array('f', [3.140000104904175, 6.28000020980835, 12.5600004196167, 25.1200008392334])