Python Array Methods: Creating, Adding, and Removing Elements
Learn about the array module in Python and how to use various methods for managing arrays. This guide covers creating arrays with the array.array()
class, and methods for adding and removing elements to efficiently handle homogeneous data.
Python - Array Methods
The array module in Python provides an efficient way to work with arrays of basic values like characters, integers, and floating point numbers. Arrays store homogeneous data elements in a specified order, similar to lists but optimized for tasks needing compact data storage.
Python Array Class
The array class offers various methods for manipulating arrays, including adding, removing, obtaining information, and converting arrays to other data types. Below are categorized methods based on their functionality.
Creating Arrays
Arrays are created using the array.array(typecode[, initializer])
class, where typecode
specifies the type of elements (a single character), and initializer
is an optional initial value.
Adding and Removing Elements
These methods append, extend, insert, pop, and remove elements from arrays:
Syntax
import array as arr
# creating an array
numericArray = arr.array('i', [10, 5, 15, 4, 6, 20, 9])
# appending an item
numericArray.append(25)
print(numericArray)
Output
array('i', [10, 5, 15, 4, 6, 20, 9, 25])
- append(x): Appends a new item with value
x
to the end of the array. - extend(iterable): Appends items from
iterable
to the end of the array. - insert(i, x): Inserts a new item with value
x
before positioni
. - pop([i]): Removes and returns the item with index
i
. Ifi
is not specified, removes and returns the last item. - remove(x): Removes the first occurrence of
x
from the array.
Information and Utility Methods
These methods provide information about arrays and perform utility operations:
- buffer_info(): Returns a tuple
(address, length)
giving the current memory address and the length of the buffer used to hold the array’s contents. - count(x): Returns the number of occurrences of
x
in the array. - index(x[, start[, stop]]): Returns the smallest index where
x
is found in the array. Optionalstart
andstop
arguments specify a subrange to search.
Manipulating Array Elements
These methods manipulate array elements, such as reversing the order of items or byteswapping values:
- reverse(): Reverses the order of items in the array.
- byteswap(): "Byteswaps" all items of the array, useful for reading data from a file written on a machine with a different byte order.
Conversion Methods
These methods convert arrays to and from bytes, files, lists, and Unicode strings:
- frombytes(buffer): Appends items from a bytes-like object, interpreting its content as an array of machine values.
- tobytes(): Converts the array to a bytes representation.
- fromfile(f, n): Reads
n
items from the file objectf
and appends them to the array. - tofile(f): Writes all items to the file object
f
. - fromlist(list): Appends items from
list
to the array. - tolist(): Converts the array to a list with the same items.
- fromunicode(s): Extends the array with data from the given Unicode string (only for arrays with type code 'u').
- tounicode(): Converts the array to a Unicode string (only for arrays with type code 'u').