In Python, both arrays and lists are used to store collections of items,
but they differ in data type restriction, memory efficiency, and performance.
Letโs explore the differences in detail ๐
๐น Overview
| Feature | List | Array |
|---|---|---|
| Module / Type | Built-in data type | Comes from array module or NumPy |
| Syntax | my_list = [1, 2, 3] | import array โ arr = array.array('i', [1, 2, 3]) |
| Data Type | Can store different data types (heterogeneous) | Can store only one data type (homogeneous) |
| Data Type Declaration | Not required | Must specify type code (e.g., 'i' for int, 'f' for float) |
| Flexibility | More flexible, dynamic | More memory-efficient and faster for numerical data |
| Performance | Slower for numeric computation | Faster for numeric operations (less overhead) |
| Usage | General-purpose container | Used mainly for numeric computation or memory optimization |
| Example Data | [10, "Hello", 3.14] | [10, 20, 30] (all same type) |
๐น 1. Python List Example
Lists are dynamic arrays that can hold any type of data โ integers, strings, floats, objects, etc.
# Creating a List
my_list = [10, "Python", 3.14, True]
print(my_list)
# Modifying list
my_list.append("New Item")
print(my_list)Output:
[10, 'Python', 3.14, True]
[10, 'Python', 3.14, True, 'New Item']โ Key Point: Lists can contain mixed data types and are built into Python โ no import needed.
๐น 2. Python Array Example
Python also provides an array module (different from NumPy arrays).
Arrays can hold only one data type, and you must declare the type code at creation.
import array
# Creating an array of integers
arr = array.array('i', [10, 20, 30, 40])
print(arr)
# Adding new element
arr.append(50)
print(arr)Output:
array('i', [10, 20, 30, 40])
array('i', [10, 20, 30, 40, 50])โ Key Point: All elements must be of the same data type, defined by the type code:
| Type Code | C Type | Python Type | Example |
|---|---|---|---|
'i' | signed int | int | array('i', [1, 2, 3]) |
'f' | float | float | array('f', [1.1, 2.2]) |
'd' | double | float | array('d', [1.11, 2.22]) |
'u' | Py_UNICODE | str (Unicode char) | array('u', ['a', 'b']) |
๐น 3. Homogeneity Example
Lists allow mixed types:
lst = [1, "Python", 3.14]
print(lst)Output:
[1, 'Python', 3.14]Arrays allow only one data type:
from array import array
arr = array('i', [1, 2, 'Python']) # โ ErrorOutput:
TypeError: an integer is required (got type str)โ Conclusion: Arrays enforce type consistency, lists do not.
๐น 4. Performance Difference
For numeric operations, arrays are more memory efficient and faster than lists.
import array, time
# Using list
lst = list(range(1000000))
start = time.time()
sum(lst)
print("List Time:", time.time() - start)
# Using array
arr = array.array('i', range(1000000))
start = time.time()
sum(arr)
print("Array Time:", time.time() - start)Output (approx):
List Time: 0.025 s
Array Time: 0.017 sโ Arrays perform better with large numeric datasets.
๐น 5. Mutability
Both lists and arrays are mutable, meaning you can modify elements.
lst = [1, 2, 3]
lst[0] = 99
print(lst) # [99, 2, 3]
import array
arr = array.array('i', [1, 2, 3])
arr[0] = 99
print(arr) # array('i', [99, 2, 3])โ Both allow modification of elements after creation.
๐น 6. Numeric Computation โ Use NumPy Arrays
For scientific or mathematical operations, use NumPy arrays (from the numpy library).
They are faster and support vectorized operations.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)Output:
[5 7 9]โ NumPy arrays are ideal for data science, machine learning, and numerical computation.
๐งพ Summary Table
| Feature | List | Array (array module) |
|---|---|---|
| Import Required | No | Yes (import array) |
| Data Type | Mixed (heterogeneous) | Same (homogeneous) |
| Syntax | [1, 2, "Python"] | array('i', [1, 2, 3]) |
| Performance | Slower | Faster for numbers |
| Memory Usage | Higher | Lower |
| Flexibility | Very flexible | Type-restricted |
| Best For | General-purpose storage | Numeric and memory-efficient operations |
| Mutability | Mutable | Mutable |
| Example Use Case | Storing user data or objects | Storing numeric data efficiently |
โ In short:
- Lists โ Store different types of elements; easy and flexible.
- Arrays โ Store only one data type; faster and memory-efficient.
- NumPy arrays โ Used for scientific computing, offering superior speed and functionality.
๐งฉ Example Summary
# List
my_list = [1, "Hello", 3.14]
print(my_list)
# Array
from array import array
my_array = array('i', [1, 2, 3])
print(my_array)Output:
[1, 'Hello', 3.14]
array('i', [1, 2, 3])โ
Conclusion:
Python lists are general-purpose, while arrays (from array or numpy) are optimized for numeric computation and memory efficiency.
