← Back to Chapters

Python Arrays

? Python Arrays

? Quick Overview

In Python, arrays are used to store multiple values of the same data type in a single variable. They come from the built-in array module and are more memory-efficient than lists when you are working with large amounts of numeric data.

? Key Concepts

  • Module based: Python arrays live in the array module, not built-in like lists.
  • Type code: Every array has a single data type, specified using a one-letter type code (for example, 'i' for signed integers).
  • Homogeneous data: All elements in an array must be of the same type.
  • Memory efficiency: Arrays use less memory than lists for large numeric datasets.
  • List-like operations: You can index, slice, append, insert, remove, and loop just like with lists.

? Syntax and Theory

To use arrays, you must first import the array module. Then you create an array by specifying a type code and an initial iterable of values:

Basic pattern:

array.array(typecode, iterable) # create an array with a type code and initial values

Common integer type codes:

  • 'i' – signed integer
  • 'I' – unsigned integer
  • 'f' – floating point
  • 'd' – double precision floating point

? Code Examples

? Creating an Integer Array

First, import the module and create an array of integers using the appropriate type code.

? View Code Example
import array

# Create an array of integers
numbers = array.array('i', [10, 20, 30, 40])
print(numbers)  # array('i', [10, 20, 30, 40])

? Accessing Array Elements

You can access elements by index, just like with lists. Indexing starts from 0.

? View Code Example
print(numbers[0])  # 10
print(numbers[2])  # 30

✏️ Modifying Array Elements

Change the value at a specific index by assigning a new value to that position.

? View Code Example
numbers[1] = 25  # change value at index 1
print(numbers)  # array('i', [10, 25, 30, 40])

➕ Adding and Removing Items

Arrays support common list-like methods such as append(), insert(), pop(), and remove().

? View Code Example
numbers.append(50)       # Add at end
numbers.insert(1, 15)    # Insert at index 1
numbers.pop()            # Remove last element
numbers.remove(30)       # Remove specific element
print(numbers)  # show final array contents

? Looping Through an Array

Use a for loop to visit each element in the array.

? View Code Example
# Loop through each value in the array
for num in numbers:
    print(num)

? Live Output and Explanation

? Sample Output Walkthrough

If you run all the examples in order, you will see something like this (values may vary based on your edits):

array('i', [10, 20, 30, 40])
10
30
array('i', [10, 25, 30, 40])
array('i', [10, 15, 25, 40, 50])
10
15
25
40
50

Notice how:

  • The first line shows the internal representation of the array object.
  • The next lines show values accessed by index.
  • After modification and insert/remove operations, the printed array reflects the new sequence of values.
  • The final block comes from looping through the array and printing each element.

✅ Tips

  • Use arrays when you need efficiency with numeric data and a fixed data type.
  • For mixed data types or general-purpose collections, prefer list over array.array.
  • For very large numeric datasets or scientific computing, consider NumPy arrays for even better performance and more features.
  • Always pick the correct type code so that your values fit without overflow or precision loss.

? Try It Yourself

  • Create an array of floats (type code 'f') and print all its elements.
  • Insert a new element at the beginning of an integer array using insert(0, value).
  • Ask the user for numbers, store them in an array, and then remove a specific number entered by the user.
  • Loop through an array and print only the even numbers.