Python array Module - A Complete Guide

In this guide, you'll explore Python's array module, which creates efficient arrays of fixed types. Learn its functions and examples for optimized storage.

The array module in Python provides an array data structure that is more efficient in terms of memory usage compared to a standard list. Arrays in this module are optimized for numeric data storage and manipulation.

Table of Contents

  1. Introduction
  2. Array Types
  3. Creating Arrays
  4. Array Methods
    • append
    • extend
    • insert
    • pop
    • remove
    • index
    • reverse
    • buffer_info
    • count
  5. Examples
    • Basic Array Operations
    • Slicing Arrays
    • Using Array Methods
  6. Real-World Use Case
  7. Conclusion
  8. References

Introduction

The array module provides a way to create arrays of uniform data types, making it more efficient than lists when working with large amounts of numeric data. Arrays created using this module are more memory-efficient because they do not store type information for each element, unlike lists.

Array Types

The array module supports various data types. Here are some common type codes:

  • 'b': signed char (1 byte)
  • 'B': unsigned char (1 byte)
  • 'h': signed short (2 bytes)
  • 'H': unsigned short (2 bytes)
  • 'i': signed int (2 bytes)
  • 'I': unsigned int (2 bytes)
  • 'l': signed long (4 bytes)
  • 'L': unsigned long (4 bytes)
  • 'f': float (4 bytes)
  • 'd': double (8 bytes)

Creating Arrays

You can create an array by specifying the type code and an optional initializer list.

import array

arr = array.array('i', [1, 2, 3, 4, 5])
print(arr)

Output:

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

Array Methods

append

The append method adds an element to the end of the array.

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

Output:

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

extend

The extend method extends the array by appending elements from an iterable.

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

Output:

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

insert

The insert method inserts an element at a specified position.

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

Output:

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

pop

The pop method removes and returns the element at a specified position.

import array
arr = array.array('i', [1, 2, 3, 4])
print(arr.pop(2))
print(arr)

Output:

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

remove

The remove method removes the first occurrence of a specified element.

import array
arr = array.array('i', [1, 2, 3, 4])
arr.remove(3)
print(arr)

Output:

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

index

The index method returns the index of the first occurrence of a specified element.

import array
arr = array.array('i', [1, 2, 3, 4])
print(arr.index(3))

Output:

2

reverse

The reverse method reverses the order of the elements in the array.

import array
arr = array.array('i', [1, 2, 3, 4])
arr.reverse()
print(arr)

Output:

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

buffer_info

The buffer_info method returns a tuple containing the memory address and the length of the array.

import array
arr = array.array('i', [1, 2, 3, 4])
print(arr.buffer_info())

Output:

(1726191878832, 4)

count

The count method returns the number of occurrences of a specified element in the array.

import array
arr = array.array('i', [1, 2, 3, 2, 4])
print(arr.count(2))

Output:

2

Examples

Basic Array Operations

Create an array and perform basic operations.

import array

arr = array.array('i', [1, 2, 3, 4])
arr.append(5)
arr.extend([6, 7])
arr.insert(0, 0)
print(arr)

Output:

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

Slicing Arrays

Arrays can be sliced similarly to lists.

import array

arr = array.array('i', [1, 2, 3, 4, 5])
print(arr[1:4])

Output:

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

Using Array Methods

Demonstrate the use of various array methods.

import array

arr = array.array('i', [1, 2, 3, 4, 5])
arr.pop(2)
arr.remove(4)
arr.reverse()
print(arr)

Output:

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

Real-World Use Case

Efficient Numeric Storage

When dealing with large datasets of numeric values, using arrays can significantly reduce memory usage compared to lists. For example, if you are processing large numerical datasets like sensor data or financial records, arrays can be more efficient.

import array
import random

# Create an array of 1 million random integers
data = array.array('i', (random.randint(0, 100) for _ in range(10**6)))
print(data[:10])  # Print the first 10 elements

Output:

array('i', [0, 8, 30, 7, 60, 37, 53, 50, 88, 76])

Conclusion

The array module in Python provides a space-efficient way to store and manipulate sequences of uniform data types. This makes it an excellent choice for handling large amounts of numerical data, offering both performance and memory benefits over standard lists.

References

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare