Menu Close

Python Array Module Tutorial

In this article, we are going to learn all about the Python array module with the help of examples.Python array module is the best built-in module when you want to work with the array.

To understand this example you should have basic knowledge of Python Programming.

Python Array Module

Python array is the built-in module to work with an array in Python applications. The array is a collection of elements of the same data type.
The array is most popular in programming languages like C, Java, JavaScript, and so on. To use array in python you have to import the array module using the import keyword.

Array Type Codes Table


Type CodesC TypesPython TypesMinimum size of bytes
‘b’signed characterint1
‘B’unsigned characterint1
‘h’signed shortint2
‘H’unsigned shortint2
‘i’signed intint2
‘I’unsigned intint2
‘l’signed longint4
‘L’unsigned longint4
‘q’signed long longint8
‘Q’unsigned long longint8
‘f’floatfloat4
‘d’doubledouble8

Attributes of array module

To get all the attributes of array module you can use dir() function.

# import array with another name arr
import array as arr
x = dir(arr)
print(x)

array.typecodes

array.typecodes attribute return the all the available type codes.

# Return a string with all the available typecodes.
typecodes = arr.typecodes
print(typecodes)

array.typecode

array.typecode attribute return the type code that can be used to create the array.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7])
print(result)


# Return type code that can be used to create the above array.
typecode = result.typecode
print(typecode)

array.itemsize

array.itemsize attribute return the length of bytes of one array.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7])
print(result)

typecode = result.itemsize
print(typecode)

array.append(x)

array.append(x) method is used to add new item of value x in the array.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7])
print(result)


# append a new item to the array.
result.append(8)
print(result)

array.buffer_info()

array.buffer_info() method returns the tuple (address, length) that give the current memory address and total length of items of an array.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7])
print(result)


# array information.
print(result.buffer_info())

array.count(x)

array.count(x) method return the number of occurrence of x in an array.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7, 7, 4])
print(result.count(7))

array.extends(iterable)

This method is used to append the items from iterable to the end of the array.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7])
print(result)
result.extend([8,9,10])
print(result)

array.index(i)

array.index(i) method is used to return the return the first occurrence of item i.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7, 4])
print(result.index(4))

array.insert(i, x)

Insert a new item with value x in the array before position i.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7, 4])
result.insert(4, 22)
print(result)

array.pop([i])

Removes the item with the index i from the array and returns it.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7, 4])
x =  result.pop(4)
print(x)

array.remove(x)

Remove the first occurrence of the array from the array.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7, 4])
result.remove(4)
print(result)

Array vs List

Some people are confused about what is difference between Python array and Python list.

Python array:- Python array is just like Python list, The only difference between both is, Python list contains items with different data types but Python array contains items with the same data type.

import array as arr
result = arr.array('i', [1,2,3,4,5,6,7])
print(result)

Python list:- Python list is data structure that is ordered, mutable or changeable which can contain element of any data type like int, float, and string etc.

x = [1, 2, 3.5, 'Python']
print(x)

Conclusion

In this article, you have learned all about the Python array module with the help of examples.
If you want to work with an array in your Python application, Then the python array module is going to be best for you.
If you like this article, please share and keep visiting for further Python built-in modules.

Reference:- Click Here

Python Platform Module
Python JSON Module

Related Posts