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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay