Industry Ready Java Spring Boot, React & Gen AI — Live Course
PythonArrays

Basics Of Array


An array in Python is a data structure used to store multiple values of the same data type in a single, contiguous block of memory. Arrays are particularly useful when working with numeric data, where uniform data types and memory efficiency are important.

Unlike lists, arrays are designed for numeric computation, and thus they are more memory-efficient when dealing with large datasets of numbers.

List_and_Arrays

Need of Arrays

Arrays address several practical programming needs:

  • Efficiently store and manipulate large collections of numeric data
  • Perform operations on data of the same type without repeated type checks
  • Use less memory compared to lists when dealing with homogeneous data
  • Interface easily with low-level languages (like C) and system-level APIs

For simple array operations, Python provides the built-in array module. For advanced numerical computations, libraries like NumPy are recommended.

Arrays_in_Python


Importing the Array Module

Before creating arrays, you must import the array module.

import array

# OR (alias)
import array as arr

# OR (import all functions)
from array import *

Creating an Array

Arrays are created using the array() constructor. You must specify:

  1. Type code (defines the data type)
  2. Initial values (as a list or iterable)

Syntax

array(typecode, [values])

Example

from array import *

vals = array('i', [5, 9, 8, 4, 2])
print(vals)

Explanation:

  • 'i' → Type code for signed integers
  • [5, 9, 8, 4, 2] → Initial values

All elements are stored sequentially in memory, making access and processing faster.


Type Codes in Python Arrays

Python arrays use type codes to enforce single-type storage.

Type CodeC TypePython TypeSize (bytes)Description
'b'signed charint1Signed integer
'B'unsigned charint1Unsigned integer
'i'signed intint2 or 4Default integer
'I'unsigned intint2 or 4Non-negative integer
'f'floatfloat4Floating point
'd'doublefloat8Double precision float
'u'Py_UNICODEchar2Unicode character

Signed vs Unsigned

  • Signed integers can store both positive and negative numbers.
  • Unsigned integers can store only non-negative values.
from array import *

# Signed integers (allows negative values)
vals = array('i', [5, 9, -8, 4, 2])

# Unsigned integers (error if negative value used)
vals_unsigned = array('I', [5, 9, 8, 4, 2])
print(vals)

Indexing and Accessing Elements

Array elements can be accessed using index numbers, starting from 0.

Array

vals = array('i', [5, 9, 8, 4, 2])

print(vals[0])   # 5
print(vals[3])   # 4

Get Array Length

print(len(vals))   # Output → 5

Array of Characters

vals = array('u', ['a', 'e', 'i', 'o', 'u'])
print(vals)
# Output → array('u', 'aeiou')

Traversing Arrays (Fetching Values)

There are multiple ways to fetch and display values from an array.

Using For Loop (unknown length)

for i in range(len(vals)):
    print(vals[i])

Using For Loop (with known length)

for i in range(5):
    print(vals[i])

Using Direct Iteration

for e in vals:
    print(e)

Using While Loop

i = 0
while i < len(vals):
    print(vals[i])
    i += 1

Summary

  • Arrays are memory-efficient and ideal for handling numeric data.
  • They provide better performance for numerical operations compared to lists.
  • Arrays store only one data type, enforced through type codes.
  • The array module must be explicitly imported before use.
  • For advanced numerical and scientific computations, NumPy is generally the preferred option.

Written By: Muskan Garg

How is this guide?

Last updated on