
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
- What is an Array in Python?
- Arrays vs Lists
- Creating Arrays
- Accessing Array Elements
- Modifying Arrays
- Array Methods
- When to Use Arrays
- 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
Feature | Arrays | Lists |
---|---|---|
Data Types | Only one type allowed | Any mix of types allowed |
Memory | More efficient | Less efficient |
Flexibility | Less flexible | More flexible |
Use Case | Large numeric datasets | General-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
Code | Type | Example |
---|---|---|
‘i’ | integer | array('i', [1,2,3]) |
‘f’ | float | array('f', [1.0,2.0]) |
‘d’ | double | array('d', [1.0,2.0]) |
‘u’ | Unicode | array('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
Method | Description | Example |
---|---|---|
append() | Adds element at end | arr.append(5) |
extend() | Adds multiple elements | arr.extend([5,6]) |
insert() | Inserts at specific position | arr.insert(1, 10) |
remove() | Removes first matching value | arr.remove(10) |
pop() | Removes element at given index | arr.pop(2) |
index() | Returns index of first match | arr.index(20) |
count() | Counts occurrences of value | arr.count(10) |
reverse() | Reverses the array | arr.reverse() |
tolist() | Converts array to Python list | arr.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! 🧠
- How do you create an array of floats?
- What method removes an element by value?
- When should you use arrays instead of lists?