DEV Community

Ravi Shankar
Ravi Shankar

Posted on

Array Methods

Array are used to store elements of same data type.
We import array module to use arrays in python.

import array as arr
arr_1 = arr.array('i', [10,20,30,40])
print(arr_1)
Enter fullscreen mode Exit fullscreen mode
  • append()

append() function is used to add an element at the end of the array.

import array as arr
arr_1 = arr.array('f', [10,11,12,13])
arr_1.append(14)
print(arr_1)
Enter fullscreen mode Exit fullscreen mode
  • reverse()

Reverses the order of an array.

import array as arr
arr_1 = arr.array('d', [10,11,12,13])
arr_1.reverse()
print(arr_1)
Enter fullscreen mode Exit fullscreen mode
  • pop()

pop() function is used to remove an element from the specified position.

import array as arr
arr_1 = arr.array('l', [10,11,12,13])
arr_1.pop(2)
print(arr_1)
Enter fullscreen mode Exit fullscreen mode
  • count()

Returns the count of given number.

import array as arr
arr_1 = arr.array('f', [10,11,10,10])
print(arr_1.count(10))
Enter fullscreen mode Exit fullscreen mode
  • insert()

Adds an element at the specified position in the array.

import array as arr
arr_1 = arr.array('i', [10,11,12,13])
arr_1.insert(4,14)
print(arr_1)
Enter fullscreen mode Exit fullscreen mode
  • remove()

Removes the first element with the specified value.

import array as arr
arr_1 = arr.array('d', [10,11,10,12,13])
arr_1.remove(10)
print(arr_1)
Enter fullscreen mode Exit fullscreen mode
  • index()

Returns the index of the first element in an array with the specified value.

import array as arr
arr_1 = arr.array('i', [10,20,30,40,10])
print(arr_1.index(10))
Enter fullscreen mode Exit fullscreen mode
  • sorted()

sorts the array in the ascending order.

import array as arr
arr_1 = arr.array('f', [19, 14, 16, 12, 10, 9])
print(sorted(arr_1))
Enter fullscreen mode Exit fullscreen mode

Reference:
Array Methods

Top comments (0)