Python Arrays with Examples

Arrays are fundamental data structures that store multiple values of the same type in a single variable. In Python, arrays are different from lists and provide more efficient storage for homogeneous data. This guide covers everything you need to know about arrays in Python.

Table of Contents

  1. What is an Array in Python?
  2. Arrays vs Lists
  3. Creating Arrays
  4. Accessing Array Elements
  5. Modifying Arrays
  6. Array Methods
  7. When to Use Arrays
  8. Conclusion

1. What is an Array in Python?

An array is a collection of items stored at contiguous memory locations. Unlike lists, arrays in Python can only store elements of the same data type, making them more memory efficient.

Key Features of Arrays

✔ Fixed-size (size defined at creation)
✔ Stores elements of the same type
✔ More memory efficient than lists for numeric data
✔ Supports basic operations like indexing, slicing


2. Arrays vs Lists

FeatureArraysLists
Data TypesOnly one type allowedAny mix of types allowed
MemoryMore efficientLess efficient
FlexibilityLess flexibleMore flexible
Use CaseLarge numeric datasetsGeneral-purpose storage

3. Creating Arrays

Python’s array module must be imported to use arrays.

Syntax

import array
array_name = array.array(typecode, [elements])

Common Typecodes

CodeTypeExample
‘i’integerarray('i', [1,2,3])
‘f’floatarray('f', [1.0,2.0])
‘d’doublearray('d', [1.0,2.0])
‘u’Unicodearray('u', ['a','b'])

Example

import array

# Integer array
numbers = array.array('i', [10, 20, 30, 40])
print(numbers)  # Output: array('i', [10, 20, 30, 40])

# Float array
prices = array.array('f', [9.99, 19.99, 29.99])
print(prices)   # Output: array('f', [9.99, 19.99, 29.99])

4. Accessing Array Elements

Arrays use zero-based indexing like lists.

Access by Index

numbers = array.array('i', [10, 20, 30])
print(numbers[0])  # Output: 10
print(numbers[-1]) # Output: 30 (last element)

Slicing Arrays

print(numbers[1:3])  # Output: array('i', [20, 30])

5. Modifying Arrays

Changing Elements

numbers[1] = 99
print(numbers)  # Output: array('i', [10, 99, 30])

Adding Elements

numbers.append(40)       # Add at end
numbers.insert(1, 50)    # Insert at index 1
print(numbers)  # Output: array('i', [10, 50, 99, 30, 40])

Removing Elements

numbers.remove(99)      # Remove first occurrence of 99
numbers.pop(2)         # Remove element at index 2
print(numbers)  # Output: array('i', [10, 50, 40])

6. Array Methods

MethodDescriptionExample
append()Adds element at endarr.append(5)
extend()Adds multiple elementsarr.extend([5,6])
insert()Inserts at specific positionarr.insert(1, 10)
remove()Removes first matching valuearr.remove(10)
pop()Removes element at given indexarr.pop(2)
index()Returns index of first matcharr.index(20)
count()Counts occurrences of valuearr.count(10)
reverse()Reverses the arrayarr.reverse()
tolist()Converts array to Python listarr.tolist()

Example

import array

arr = array.array('i', [10, 20, 30, 20])
print(arr.count(20))    # Output: 2
print(arr.index(30))    # Output: 2
arr.reverse()
print(arr)              # Output: array('i', [20, 30, 20, 10])

7. When to Use Arrays

Working with large numeric datasets (faster than lists)
Memory efficiency is important
Need fixed-type collections

When NOT to Use Arrays

❌ Need mixed data types (use lists instead)
❌ Require frequent size changes (lists are more flexible)


8. Conclusion

  • Arrays store homogeneous data efficiently.
  • Must specify type when creating ('i', 'f', etc.).
  • Useful for numeric computations where memory matters.
  • For general use, Python lists are usually preferred.

Now you can use arrays effectively in Python! 🚀


Quiz Time! 🧠

  1. How do you create an array of floats?
  2. What method removes an element by value?
  3. When should you use arrays instead of lists?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top