Skip to content

Arrays

In Python, the term "array" is often used interchangeably with list. However, Python provides both lists (which are more commonly used) and arrays through the array module. Here’s a quick rundown of the key types of "arrays" in Python:

1. Lists (Most Commonly Used):

A list is an ordered collection of elements, which can be of any type, including other lists.

  • Syntax:
python
my_list = [element1, element2, element3]
  • Example:
python
fruits = ["apple", "banana", "cherry"]
print(fruits)

Output:

['apple', 'banana', 'cherry']

Key Properties of Lists:

  • Lists are mutable (can be changed).
  • Lists can contain mixed data types (e.g., integers, strings, other lists).
  • You can access list items using indexing (my_list[0]), and they support slicing (my_list[1:3]).
  • Lists can be nested (lists within lists).

Common List Operations:

  • Adding elements: append(), insert()
  • Removing elements: remove(), pop(), clear()
  • Length: len(list)
  • Sorting: sort()
python
my_list = [10, 20, 30]
my_list.append(40)  # Adds 40 at the end
my_list.remove(20)  # Removes 20
print(my_list)

Output:

[10, 30, 40]

2. Arrays (from array module):

The array module in Python provides a space-efficient way to store data of the same type. Unlike lists, arrays can only hold elements of the same data type, such as all integers or all floats.

  • Syntax:
python
import array
my_array = array.array('typecode', [element1, element2, element3])

The typecode specifies the data type of the array elements. Common typecodes include:

  • 'i' for integer

  • 'f' for floating point numbers

  • Example:

python
import array

# Create an array of integers
my_array = array.array('i', [1, 2, 3, 4, 5])
print(my_array)

Output:

array('i', [1, 2, 3, 4, 5])

Key Properties of Arrays:

  • Arrays are more efficient than lists for large quantities of data of the same type.
  • They require less memory and offer faster access to elements.
  • They cannot store different types of data (e.g., a string and an integer in the same array).

3. NumPy Arrays (If working with large numerical datasets):

The NumPy library provides another type of array, which is highly optimized for numerical and scientific computing. It supports multi-dimensional arrays and matrices, as well as a wide range of mathematical functions.

  • Syntax:
python
import numpy as np
my_numpy_array = np.array([element1, element2, element3])
  • Example:
python
import numpy as np

my_numpy_array = np.array([1, 2, 3, 4])
print(my_numpy_array)

Output:

[1 2 3 4]

Key Advantages of NumPy Arrays:

  • More efficient and fast for numerical computations.
  • Supports multi-dimensional arrays (e.g., matrices).
  • Provides a rich set of functions for mathematical operations.

Comparison of Lists, Arrays, and NumPy Arrays:

  • Lists: Flexible, can store mixed data types, but not as efficient for numerical operations.
  • Arrays (from array module): More memory-efficient than lists, but only support one data type at a time.
  • NumPy Arrays: Most efficient for large numerical data, supports multi-dimensional arrays, and has a broad range of mathematical functions.

Example: Using Lists, Arrays, and NumPy Arrays

python
# List Example
my_list = [1, 2, 3, 4]
my_list.append(5)
print("List:", my_list)

# Array Example
import array
my_array = array.array('i', [1, 2, 3, 4])
my_array.append(5)
print("Array:", my_array)

# NumPy Array Example
import numpy as np
my_numpy_array = np.array([1, 2, 3, 4])
my_numpy_array = np.append(my_numpy_array, 5)
print("NumPy Array:", my_numpy_array)

Output:

List: [1, 2, 3, 4, 5]
Array: array('i', [1, 2, 3, 4, 5])
NumPy Array: [1 2 3 4 5]

J2J Institute private limited