Tutorials Logic, IN info@tutorialslogic.com

Arrays in Python array Module NumPy

Python Arrays

Python beginners usually use lists first, but Python also has an array module for compact typed numeric data.

An array stores values of one basic type, such as integers or floats, and can be useful when memory shape matters.

For scientific computing, NumPy arrays are usually the professional choice; for normal beginner programs, lists are often enough.

Arrays in Python

Python doesn't have a built-in array type like C or Java. Instead, you have three options depending on your needs:

  • list - general-purpose, mixed types, most common
  • array module - typed array, more memory-efficient than list
  • NumPy ndarray - powerful multi-dimensional arrays for math/science

Using Lists as Arrays

For most use cases, Python lists work perfectly as arrays.

Lists as Arrays

Lists as Arrays
scores = [85, 92, 78, 95, 88]

# Access by index
print(scores[0])    # 85
print(scores[-1])   # 88

# Modify
scores[2] = 80
scores.append(91)

# Iterate
for score in scores:
    print(score)

# 2D array (list of lists)
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print(matrix[1][2])  # 6

# Traverse 2D array
for row in matrix:
    for val in row:
        print(val, end=" ")
    print()

The array Module

The built-in array module provides typed arrays - all elements must be the same type. More memory-efficient than lists for large numeric data.

Type Code C Type Python Type Size
'b' signed char int 1 byte
'B' unsigned char int 1 byte
'i' signed int int 2 bytes
'I' unsigned int int 2 bytes
'l' signed long int 4 bytes
'f' float float 4 bytes
'd' double float 8 bytes

array Module

array Module
import array

# array(typecode, initializer)
# 'i' = signed int, 'f' = float, 'd' = double
nums = array.array('i', [1, 2, 3, 4, 5])
floats = array.array('f', [1.1, 2.2, 3.3])

print(nums[0])     # 1
print(nums[1:3])   # array('i', [2, 3])

nums.append(6)
nums.insert(0, 0)
nums.remove(3)
print(nums)        # array('i', [0, 1, 2, 4, 5, 6])

# Convert to list
as_list = nums.tolist()
print(as_list)     # [0, 1, 2, 4, 5, 6]

NumPy Arrays

NumPy is the go-to library for numerical computing. Its ndarray supports multi-dimensional arrays and vectorized math operations.

Create and Calculate NumPy Arrays

Create and Calculate NumPy Arrays
import numpy as np

# Create arrays
a = np.array([1, 2, 3, 4, 5])
b = np.zeros(5)           # [0. 0. 0. 0. 0.]
c = np.ones((2, 3))       # 2x3 matrix of ones
d = np.arange(0, 10, 2)   # [0 2 4 6 8]
e = np.linspace(0, 1, 5)  # [0.   0.25 0.5  0.75 1.  ]

# Shape and dimensions
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.shape)   # (2, 3)
print(matrix.ndim)    # 2
print(matrix.dtype)   # int64

# Vectorized operations (no loops needed!)
x = np.array([1, 2, 3, 4])
print(x * 2)          # [2 4 6 8]
print(x ** 2)         # [1 4 9 16]
print(x + x)          # [2 4 6 8]

# Slicing
print(matrix[0, :])   # [1 2 3] - first row
print(matrix[:, 1])   # [2 5]   - second column
print(matrix[1, 1:])  # [5 6]

# Aggregate functions
print(np.sum(x))      # 10
print(np.mean(x))     # 2.5
print(np.max(x))      # 4
print(np.std(x))      # standard deviation
Array choice check

Can You Choose an Array-Like Structure?

5 checks
  • Python doesn't have a built-in array type like C or Java.
  • Instead, you have three options depending on your needs.
  • Choose a list for general-purpose ordered values; choose a typed array only when its constraints are useful.
  • The built-in array module provides typed arrays - all elements must be the same type.
  • More memory-efficient than lists for large numeric data.

Array Module Traps

  • Choosing array before list

    Use list for general Python work. Use array only when compact same-type numeric storage matters.
  • Mixing value types

    An array has one type code, so convert or reject incompatible values before appending.
  • Confusing array with NumPy

    The array module is small and built in. NumPy arrays are a separate library for numeric computing.

Try this next

Store Typed Numbers

0 of 3 completed

  1. Create an array of integers and append three values.
  2. Write the same values in a list and an array, then explain which one is clearer for beginners.
  3. Try appending a string to an integer array and read the error.

Array Choices

Start with lists. Use array when you specifically need compact storage for same-type numeric values.

No. The built-in array module is simpler. NumPy arrays are much more powerful for numerical computing.

No. The array module stores one basic type at a time. Use a list for mixed values.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.