Arrays built-in methods – Python

Arrays in Python are homogeneous, type-code specific, memory optimized and indexed based sequential data structure. Arrays in Python are very similar to Lists. Similar to Lists, Arrays also support multiple built-in methods. Most of these methods are common and can be applied with other collection datatypes like list, tuple, set and dictionary. In this article, we will explore arrays built-in methods.

Arrays built-in methods

reverse() and colon (:) operator

We can reverse the order of elements of an array using colon (:) operator and reverse() method.

Reverse array with colon (:) operator => reversed_array = array_object[::-1]

  • array_object is an array, whose order of elements to be reversed.
  • [::-1] using colon operator and iterating it from right to left. Iterating from right to left will reverse the order of elements.
  • reversed_array => returns a new reversed array and does not make any change to original array.

reverse() method in Python is used to reverse the order of elements of a collection datatypes like list or an array. Syntax of reverse() method => array_object.reverse()

  • array_object is an array, whose order of elements to be reversed.
  • reverse() method name to reverse the order of elements from the array. reverse() method does not return any new array, but modified the original array itself.
# reverse array - reverse method
print("reverse array - reverse method")
import array as array

double_array = array.array("d", [134.1, 9267.2, 8763.3, 423.4, 273.89])   # array type-code d (double)
print(double_array)

reversed_array = double_array[::-1]   # reverse array using colon operator
print(reversed_array)

double_array.reverse()   # reverse() method
print(double_array)

In this program, we are showing reverse of array using colon (:) operator and reverse() method. We have defined an array double_array with type-code d (double).

  • Using colon (:) operator – With step = -1, we are iterating array from right to left and new array reversed_array with reverse order of elements will be returned.
  • Using double_array.reverse() => to reverse the order of elements of an array double_array.

Program Output

reverse array - reverse method
array('d', [134.1, 9267.2, 8763.3, 423.4, 273.89])   # original array elements
array('d', [273.89, 423.4, 8763.3, 9267.2, 134.1])   # reversed array elements using colon operator
array('d', [273.89, 423.4, 8763.3, 9267.2, 134.1])   # reversed array elements using reverse()

From the program output, the order of elements of an array has been reversed using colon (:) operator and reverse() method.

sorted() method

We can sort the elements of an array using sorted() method. sorted() method in Python is used to sort the elements of a collection datatypes like list or an array. Syntax of sorted() method => sorted_array = sorted(array_object)

  • array_object is an array, whose elements to be sorted.
  • sorted() is the method name to sort the elements of an array. sorted() method returns a new array sorted_array and does not modify the original array.
# sorting of an array - sorted() method
print("sorting of an array - sorted() method")
import array as array

double_array = array.array("d", [134.1, 9267.2, 8763.3, 423.4, 273.89])   # array type-code d (double)
print(double_array)

sorted_array = sorted(double_array)   # sorted() method
print(sorted_array)

We have defined an array double_array with type-code d (double). Using sorted(double_array) => to sort the elements of an array. A new array sorted_array is returned with elements in sorted order.

Program Output

sorting of an array - sorted() method
array('d', [134.1, 9267.2, 8763.3, 423.4, 273.89])   # original array
[134.1, 273.89, 423.4, 8763.3, 9267.2]    # sorted array

From the program output, sorted array has elements in sorted order.

len(), sum(), min(), max() methods

Python’s common built-in methods can be used with array object as well.

  • len() method calculates the length (total number of elements) of an array. It returns the length of the array. Syntax => array_length = len(array_object)
  • sum() method calculates the total sum of all the elements of an array. It returns the sum of all elements in the array. Syntax => elements_sum = sum(array_object)
  • min() method finds out the smallest item in an array. It returns the smallest item in the array. Syntax => min_element = min(array_object)
  • max() method finds out the largest item in an array. It returns the largest item in the array. Syntax => max_element = max(array_object)
# array built-in methods
print("array built-in methods")
import array as array

double_array = array.array("d", [134.1, 9267.2, 8763.3, 423.4, 273.89])  # array type-code d (double)
print(double_array)

array_length = len(double_array)   # calculate total number of elements in an array
print(array_length)

elements_sum = sum(double_array)  # calculate total sum of all the elements in an array
print(elements_sum)

min_element = min(double_array)  # find out smallest element in an array
print(min_element)

max_element = max(double_array)  # find out largest element in an array
print(max_element)

In this program, we have defined an array double_array with type-code d (double). We are using:

  • len(double_array) = > to calculate the length (total number of elements) in an array
  • sum(double_array) = > to calculate total sum of all the elements in an array
  • min(double_array) = > to calculate smallest element in an array
  • max(double_array) = > to calculate largest element in an array

Program Output

array built-in methods
array('d', [134.1, 9267.2, 8763.3, 423.4, 273.89])   # original array elements
5                     # length - total number of elements in an array
18861.89      # sum - total sum of all elements in an array
134.1             # min - smallest element in an array
9267.2           # max - largest element in an array

count(), itemsize, tolist() methods

There are other built-in methods that can be used with array objects.

  • count(element) method is used to count the number of occurrences of element in the array. Here element is whose count to be calculated in the array. Syntax => element_count = array_object.count(element)
  • itemsize attribute is part of Python’s array module. This is used to return the size (in bytes) of each element in the array. This is useful when working with binary data or when you need to know the memory footprint of the array elements. Syntax => array_itemsize = array_object.itemsize
  • tolist() method converts the array to an ordinary list with the same items. Syntax => created_list = array_object.tolist()
# array built-in methods
print("array built-in methods")
import array as array

signed_short_array = array.array("h", [13, 92, 87, 42, 27, 87])    # array type-code d (double)
print(signed_short_array)

element_count = signed_short_array.count(87)   # count number of occurrences of 87
print(element_count)

array_itemsize = signed_short_array.itemsize   # return size in byte of each element
print(array_itemsize)

short_list = signed_short_array.tolist()   # convert array elements to a list
print(short_list)

In this program, we have defined an array signed_short_array with type-code h (signed short). We are using:

  • signed_short_array.count(87) => to calculate the total occurrences of element 87 in an array
  • signed_short_array.itemsize => to return the size (in bytes) of each element
  • signed_short_array.tolist() => to convert array elements into a list

Program Output

array built-in methods
array('h', [13, 92, 87, 42, 27, 87])   # original array elements
2            # occurrences of element 87
2            # size in byte of array elements
[13, 92, 87, 42, 27, 87]    # array converted to list

Summary

In this article, we have explored various built-in methods of Python, that can be used with array as well. Following sections were explored:

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *