Course

Python Arrays: Syntax, Usage, and Examples

Python arrays provide a structured way to store multiple values in a single variable. While Python doesn’t have built-in arrays in the same way as some other languages, it supports them through modules like array and via more commonly used lists. Understanding how to work with Python arrays helps you manage collections of data, especially when performance and memory efficiency matter.

Although lists are more versatile, arrays Python offers through its standard library and third-party modules are ideal for working with large numeric datasets.

How to Create an Array in Python

To create an array in Python, you can use either the built-in list type or the array module. Here’s how both approaches look:

Using Lists (Most Common)

python
numbers = [1, 2, 3, 4, 5]

This creates a list that behaves similarly to arrays in other languages.

Using the array Module

python
import array nums = array.array('i', [1, 2, 3, 4])
  • 'i' is the type code for integers.
  • This method uses less memory but requires elements to be the same type.

When to Use Python Arrays

Store a Sequence of Elements

Arrays are great for keeping track of related data items:

python
temperatures = [72, 68, 75, 70]

Each element is accessible by its index.

When Performance Matters

Using array.array can be faster and more memory-efficient:

python
import array data = array.array('f', [1.1, 2.2, 3.3])

Good for scientific applications and large numerical datasets.

Use Arrays to Simplify Operations on Collections

Arrays let you manipulate entire groups of values at once using indexing and slicing.

Examples of Python Arrays in Practice

Accessing Elements (Array Indexing in Python)

python
fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Output: apple

Indexing starts at 0. You can also use negative indices:

python
print(fruits[-1]) # Output: cherry

Slicing an Array (Python Array Slice)

python
data = [10, 20, 30, 40, 50] subset = data[1:4] print(subset) # Output: [20, 30, 40]

You can omit the start or end index:

python
print(data[:3]) # [10, 20, 30] print(data[2:]) # [30, 40, 50]

Checking Python Array Length

Use the len() function to find the number of elements:

python
colors = ["red", "green", "blue"] print(len(colors)) # Output: 3

The len of array in python is one of the most commonly used operations when iterating or validating inputs.

Sorting an Array (Python Sort Array)

You can sort arrays using the sorted() function or .sort() method:

python
nums = [4, 1, 3, 2] nums.sort() print(nums) # [1, 2, 3, 4]

For temporary sorting:

python
print(sorted(nums, reverse=True)) # [4, 3, 2, 1]

Learn More About Python Arrays

How to Create an Array in Python Using NumPy

If you need advanced array features like matrix operations, NumPy is the go-to library:

python
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr) # Output: [1 2 3 4]

NumPy arrays are faster and more feature-rich than native arrays.

Array Size Python Tip

For arrays created with NumPy or array.array, you can get their size using:

python
import array nums = array.array('i', [1, 2, 3]) print(len(nums)) # 3

Or for NumPy:

python
print(arr.size) # Number of elements in the array

Python Array vs List

  • List: Can hold mixed data types, flexible, widely used.
  • Array (from array module): Requires all elements to be the same type, more efficient.
  • NumPy Array: Used for mathematical and scientific computing, supports multi-dimensional arrays.
python
list_example = [1, "a", 3.5] array_example = array.array('i', [1, 2, 3]) # All integers

Use lists for general purposes, arrays for performance-critical number crunching.

Modify Array Elements

You can change values using indexing:

python
numbers = [10, 20, 30] numbers[1] = 25 print(numbers) # [10, 25, 30]

Append and Remove Items

python
items = [1, 2, 3] items.append(4) print(items) # [1, 2, 3, 4] items.pop() print(items) # [1, 2, 3]

For arrays from the array module:

python
import array nums = array.array('i', [1, 2, 3]) nums.append(4) nums.pop()

Looping Through Arrays

python
for item in ["a", "b", "c"]: print(item)

Or with index:

python
for i in range(len(items)): print(items[i])

Copying Arrays

Use slicing to make a shallow copy:

python
original = [1, 2, 3] copy = original[:]

For deeper structures, use the copy module.

Arrays with Conditional Logic

python
scores = [55, 80, 72, 90] passed = [score for score in scores if score >= 60] print(passed) # [80, 72, 90]

List comprehensions are a powerful way to work with arrays in Python.

Nesting Arrays

Python arrays (lists) can be nested:

python
matrix = [[1, 2], [3, 4]] print(matrix[0][1]) # Output: 2

Ideal for representing grids, tables, and matrices.

Python arrays, whether implemented as lists or with the array module, are a fundamental data structure used for storing and manipulating collections of items. From slicing and indexing to sorting and looping, mastering the basics of arrays Python offers lets you manage data more effectively and write clear, efficient code. Understanding array size in Python, how to create them, and the differences between arrays and lists gives you a solid foundation for working with structured data in any project.