DEV Community

Manisha Kundrapu
Manisha Kundrapu

Posted on

Python Array Methods

In Python, an array is a container that holds a fixed number of items of the same type.

Arrays are useful for working with large amounts of data that need to be accessed quickly.

Python provides a built-in module called "array" for working with arrays.

Here are some commonly used methods for working with arrays in Python:

1. Creating an array:

To create an array in Python, you first need to import the "array" module.

Then, you can create an array using the "array" function and passing it a type code and a sequence of values.

‘b’ – signed char

‘B’ – unsigned char

‘d’ – double

‘f’ – float

‘i’ – signed int

‘I’ – unsigned int

‘h’ – signed short

‘H’ – unsigned short

‘l’ – signed long

‘L’ – unsigned long

Example:

import array
ar = array.array('i', [11,12,13,14])
print(ar)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [11,12,13,14])

2. append():

This method adds the specified value to the end of the array.

Syntax : array_name.append(item)

Example:

import array
arr = array.array('i', [1, 2, 3])
arr.append(4)
print(arr)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [1, 2, 3, 4])

3. remove():

This method eliminates the first instance of the supplied value from the array.

Syntax : array_name.remove(item)

Example:

import array
arr = array.array('i', [1, 2, 3, 2])
arr.remove(2)
print(arr)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [1, 3, 2])

4. sort():

The array's elements are sorted using this method.

Syntax : array_name.sort()

Example:

import array
arr = array.array('i', [5, 2, 1, 4, 3])
arr.sort()
print(arr)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [1, 2, 3, 4, 5])

5. extend():

This method adds the elements from the specified iterable to the end of the array.

Syntax : extend(iterable)

Example:

import array
arr1 = array.array('i', [1, 2, 3])
arr2 = array.array('i', [4, 5, 6])
arr1.extend(arr2)
print(arr1)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [1, 2, 3, 4, 5, 6])

6. insert():

This method inserts the specified value at the specified index.

Syntax : insert(index, value)

Example:

import array
arr = array.array('i', [1, 2, 3])
arr.insert(1, 4)
print(arr)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [1, 4, 2, 3])

7. pop():

The element at the supplied index is removed and returned by this method. If no index is specified, it removes and returns the last element.

Syntax : pop(index=-1)

Example:

import array
arr = array.array('i', [1, 2, 3])
removed_element = arr.pop(1)
print(arr)
print(removed_element)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [1, 3])
2

8. index():

The index() method returns the index of the first occurrence of a specified element in the array.

Syntax : array.index(element[, start[, end]])

Example:

import array
a = array.array('i', [1, 2, 3, 2])
print(a.index(2)) 
Enter fullscreen mode Exit fullscreen mode

Output: 1

9. reverse():

The order of the array's elements is reversed by this method.

Syntax : reverse()

Example:

import array
arr = array.array('i', [1, 2, 3])
arr.reverse()
print(arr)
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [3, 2, 1])

10. count():

This method returns the number of times the specified value appears in the array.

Syntax : count(value)

Example:

import array
arr = array.array('i', [1, 2, 3, 2])
count = arr.count(2)
print(count)
Enter fullscreen mode Exit fullscreen mode

Output: 2

11. typecode:

The typecode attribute of an array returns the type code character used to create the array.

Syntax : array.typecode

Example:

import array
a = array.array('i', [1, 2, 3, 4])
print(a.typecode)
Enter fullscreen mode Exit fullscreen mode

Output: "i"

12. itemsize:

The itemsize attribute of an array returns the size of each element in bytes.

Syntax : array.itemsize

Example:

import array
a = array.array('i', [1, 2, 3, 4])
print(a.itemsize)
Enter fullscreen mode Exit fullscreen mode

Output: 4

13. buffer_info():

The buffer_info() method returns a tuple representing the address of the array's buffer and the size of the buffer in bytes.

Syntax : array.buffer_info()

Example:

import array
a = array.array('i', [1, 2, 3, 4])
print(a.buffer_info()) 
Enter fullscreen mode Exit fullscreen mode

Output: (139953048219200, 16)

14. fromlist():

The fromlist() method appends a list to the end of an array.

Syntax : array.fromlist(list)

Example:

import array
ar = array.array('i', [1, 2, 3, 4])
ar.fromlist([15, 16, 17, 18])
print(ar) 
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [1, 2, 3, 4, 15, 16, 17, 18])

15. tolist():

The tolist() method returns a list containing all the elements of an array.

Syntax : array.tolist()

Example:

import array
a = array.array('i', [1, 2, 3, 4])
print(a.tolist()) 
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 4]

16. copy():

A shallow copy of the array is what is returned by the copy() method.

Syntax : array.copy()

Example:

import array
a = array.array('i', [11, 12, 13, 14])
cpy_a = a.copy()
print(cpy_a) 
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [11, 12, 13, 14])

17. clear():

The clear() method removes all the elements from the array.

Syntax : array.clear()

Example:

import array
a = array.array('i', [1, 2, 3, 4])
a.clear()
print(a) 
Enter fullscreen mode Exit fullscreen mode

Output:

array('i', [])

Top comments (0)