Python Array


In Python, arrays are used to store multiple items in a single variable.

However, the term "array" is often associated with lists in Python, as the built-in list type provides similar functionality.

For more specialized use cases, Python also offers arrays through the array module and other libraries like NumPy.

Python Lists as Arrays:

Lists in Python are versatile and can be used as dynamic arrays that can store items of different types.

Array Properties

  1. Ordered: Elements are stored in a specific order and can be accessed using indices.
  2. Mutable: Elements can be changed, added, or removed.
  3. Heterogeneous: A list can contain items of different data types.

Creating a Array List

Creating a array of names-

# Creating a list
names = ["Alice", "Bob", "Charlie"]

# Print the list to verify
print(names)

 Accessing Elements in Array

Accessing the first element in the array-

# Creating a list
names = ["Alice", "Bob", "Charlie"]

# Accessing elements
print(names[0])  # Output: Alice

Modifying Elements

Modifying the second element in the array-

# Creating a list
names = ["Alice", "Bob", "Charlie"]

# Modifying elements
names[1] = "Betty"
print(names)  # Output: ['Alice', 'Betty', 'Charlie']

Adding Elements

Adding a new element to the end of the array

# Creating a list
names = ["Alice", "Bob", "Charlie"]

# Adding elements
names.append("David")
print(names)  # Output: ['Alice', 'Bob', 'Charlie', 'David']

Removing Elements

Removing an element from the array

# Creating a list
names = ["Alice", "Bob", "Charlie"]

# Removing elements
names.remove("Charlie")
print(names)  # Output: ['Alice', 'Bob']

 Iterating Over a array

Iterating over the array to print each name-

# Creating a list
names = ["Alice", "Bob", "Charlie"]

# Iterating over a list
for name in names:
    print(name)

# Output:
# Alice
# Bob
# Charlie


Python array Module:

The array module provides an array type that is more efficient for numerical data and can only store items of the same type.

Example of array Module:

import array as arr

# Creating an array of integers
numbers = arr.array('i', [1, 2, 3, 4, 5])

# Accessing elements
print(numbers[0])  # Output: 1

# Modifying elements
numbers[1] = 10
print(numbers)  # Output: array('i', [1, 10, 3, 4, 5])

# Adding elements
numbers.append(6)
print(numbers)  # Output: array('i', [1, 10, 3, 4, 5, 6])

# Removing elements
numbers.remove(10)
print(numbers)  # Output: array('i', [1, 3, 4, 5, 6])

# Iterating over an array
for number in numbers:
    print(number)

# Output:
# 1
# 3
# 4
# 5
# 6

NumPy Arrays:

For more advanced numerical operations, the NumPy library provides the ndarray type, which is more powerful and efficient than Python lists and the array module.

Example of NumPy Arrays:
import numpy as np

# Creating a NumPy array
numbers = np.array([1, 2, 3, 4, 5])

# Accessing elements
print(numbers[0])  # Output: 1

# Modifying elements
numbers[1] = 10
print(numbers)  # Output: [ 1 10  3  4  5]

# Adding elements (note: this creates a new array)
numbers = np.append(numbers, 6)
print(numbers)  # Output: [ 1 10  3  4  5  6]

# Removing elements (note: this creates a new array)
numbers = np.delete(numbers, 1)  # Remove element at index 1
print(numbers)  # Output: [1 3 4 5 6]

# Iterating over a NumPy array
for number in numbers:
    print(number)

# Output:
# 1
# 3
# 4
# 5
# 6

Overview:

  • Python Lists: Versatile and can store heterogeneous data types. Useful for general-purpose arrays.
  • array Module: Provides arrays for numerical data with type restrictions for efficiency.
  • NumPy Arrays: Offer advanced capabilities for numerical computations and are highly efficient for large datasets.

Choose the appropriate array type based on the specific requirements of your task.