Python Arrays

Last Updated : 9 Jun, 2026

Python provides multiple ways to work with linear data structures, which store elements sequentially. Although Python does not have a built-in array type like some other languages, similar functionality can be achieved using:

Lists

Lists are a flexible and commonly used data structure for storing elements in sequence. Unlike arrays in other languages, list:

  • Can store mixed data types in one list
  • Elements can be added or removed easily
  • Provide functions like append(), remove(), sort(), etc.
Python
a = [1, "Hello", [3.14, "world"]]
a.append(2)  # Add an integer to the end
print(a)  

Output
[1, 'Hello', [3.14, 'world'], 2]

NumPy Arrays

NumPy arrays are a part of NumPy library, which is a tool for numerical computing. Designed for high-performance operations on large datasets and support multi-dimensional arrays and matrices, making them suitable for complex mathematical computations.

  • Multi-dimensional support: Can handle multiple dimensions, making them suitable for matrix operations and advanced mathematical tasks.
  • Broad broadcasting capabilities: Allow operations between arrays of different shapes and sizes using broadcasting.
  • Efficient storage and processing: Uses optimized memory and provides faster performance compared to lists for numerical operations.
Python
import numpy as np
a = np.array([1, 2, 3, 4])

# Element-wise operations
print(a * 2)  

# Multi-dimensional array
res = np.array([[1, 2], [3, 4]])
print(res * 2)

Output
[2 4 6 8]
[[2 4]
 [6 8]]

Note: Use NumPy arrays for complex, multi-dimensional computations. Use Python’s array module for simple, memory-efficient storage of uniform data.

Arrays

Array is a collection of elements stored at contiguous memory locations, used to hold multiple values of the same data type. Unlike Lists, which can store mixed types, arrays are homogeneous and require a typecode during initialization to define the data type.

Python
import array as arr
a = arr.array('i', [1, 2, 3])

# accessing first array
print(a[0])

# adding element to array
a.append(5)
print(a)  

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

Explanation: parameter 'i' is the typecode, it tells Python to treat the elements as signed integers ([1, 2, 3]) of a specific byte size (usually 2 or 4 bytes).

The following table outlines standard typecodes, their mapped C types and minimum memory requirements:

TypecodeC TypePython TypeMinimum Size (Bytes)
'b'signed charint1
'i'signed intint4
'f'floatfloat4
'd'doublefloat8

Create an Array

Array can be created by importing an array module. array(data_type, value_list) is used to create array with data type and value list specified in its arguments.

Python
import array as arr
a = arr.array('i', [1, 2, 3])

for i in range(0, 3):
    print(a[i], end=" ")

Output
1 2 3 
2
Python Array Index

Adding Elements

Elements can be added to an array using insert() to place a value at a specific index, or append() to add a value at the end.

Python
import array as arr
a = arr.array('i', [1, 2, 3])
print(*a)

a.insert(1, 4)  # Insert 4 at index 1
print(*a)

Output
1 2 3
1 4 2 3

Note: We have used *a and *b for unpacking the array elements.

Accessing Items

Array elements are accessed using their index with square brackets [ ]. Each item has a position starting from 0 and the index must be an integer.

Python
import array as arr
a = arr.array('i', [1, 2, 3, 4, 5, 6])

print(a[0])
print(a[3])

b = arr.array('d', [2.5, 3.2, 3.3])
print(b[1])
print(b[2])

Output
1
4
3.2
3.3

Removing Elements

Elements can be removed using remove(), which deletes the first occurrence of a value or pop(), which removes and returns an element (last by default or a specific index if provided).

Python
import array
a = array.array('i', [1, 2, 3, 1, 5])

# remove first occurence of 1
a.remove(1)
print(a)

# remove item at index 2
a.pop(2)
print(a)

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

Slicing

Slicing is used to access a specific range of elements from an array using index positions.

11
Python Index Slicing
  • Elements from beginning to a range use [:Index]
  • Elements from end use [:-Index]
  • Elements from specific Index till the end use [Index:]
  • Elements within a range, use [Start Index:End Index]
  • Print complete List, use [:].
  • For Reverse list, use [::-1].
Python
import array as arr
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = arr.array('i', a)

res = b[3:8]
print(res