30% Therapy – 40% Practice – 30% Work project

Python – Array Methods



The array module in Python offers an efficient object type for representing arrays of basic values like characters, integers, and floating point numbers. Arrays are similar to lists but it stores a collection of homogeneous data elements in order. At the time of creating array”s type is specified using a single character type code.

Array methods provides various operations on array objects, including appending, extending, and manipulating elements. These methods are used for efficient handling of homogeneous collections of basic data types, making them suitable for tasks requiring compact data storage, such as numerical computations.

Python Array Class

The array class defines several methods, including adding and removing elements, obtaining information about the array, manipulating array elements, and converting arrays to and from other data types. Below are categorized methods based on their functionality. Let”s explore and understand the functionality of each method.

Arrays are created using the array.array(typecode[, initializer]) class, where typecode is a single character that defines the type of elements in the array, and initializer is an optional value used to initialize the array.

Adding and Removing Elements

Below methods are used for appending, extending, inserting, and removing elements from arrays −

Sr.No. Methods with Description
1

Appends a new item with value x to the end of the array.

2

Appends items from iterable to the end of the array.

3

Inserts a new item with value x before position i.

4

Removes and returns the item with index i. If i is not specified, removes and returns the last item.

5

Removes the first occurrence of x from the array.

Information and Utility Methods

These methods are used for obtaining information about arrays and to perform utility operations −

Sr.No. Methods with Description
1

Returns a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold the array’s contents.

2

Returns the number of occurrences of x in the array.

3

Returns the smallest index where x is found in the array. Optional start and stop arguments can specify a sub-range to search.

Manipulating Array Elements

Following methods are used for manipulating array elements, such as reversing the array or byteswapping values.

Sr.No. Methods with Description
1

Reverses the order of the items in the array.

2

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 are used to convert arrays to and from bytes, files, lists, and Unicode strings.

Sr.No. Methods with Description
1

frombytes(buffer)

Appends items from the bytes-like object, interpreting its content as an array of machine values.

2

tobytes()

Converts the array to a bytes representation.

3

Reads n items from the file object f and appends them to the array.

4

Writes all items to the file object f.

5

Appends items from the list to the array.

6

Converts the array to a list with the same items.

7

Extends the array with data from the given Unicode string. The array must have type code ”u”.

8

Converts the array to a Unicode string. The array must have type code ”u”.

Translate »