Table of Contents
In Python an array is a data structure that stores elements of the same data type. They are useful when you want faster performance and efficient memory usage especially when dealing with large datasets.
Python offers arrays through:
- The built-in array module
- NumPy, the most popular library for numerical computing.
Array vs List in Python: What’s the Difference?
Arrays may look and function similarly to lists, but here are some key differences.
| Feature | Array | List |
|---|---|---|
| Data Type | It can store only one data type | It can store mixed data types |
| Speed | It is usually faster for numerical operations. | Slower for numericals |
| Memory | Uses less memory. | Uses more memory. |
| Use Case | It is mainly used in cases which involves numerical operations | Used for other general purposes |
How to Create an Array in Python?
There are mainly two ways to create an array.
- Importing the array module
- Creating an integer array
# Importing the array module
import array as arr
numbers = arr.array('i', [5, 12, 8, 20, 3])
print(f"Type: {type(numbers)}") # Type:
print(f"Contents: {numbers}") # Contents: array('i', [5, 12, 8, 20, 3])
# Creating an integer array
numbers = [10, 20, 30, 40]
print(f" {numbers}") # [10, 20, 30, 40]
Array Operations
1. Access elements:
Array elements can be accessed by the element indexes[ ]. The index value starts from 0 and goes till the end of the array.
Syntax: Array[index]
2. Add elements:
There are two methods:
Insert(): Elements can be added to the array by using the insert function. Insert can be used to add an element to the array at a time.
Syntax: array_name.insert(index, element)
Append: Like in list append can be used to insert elements at the end of the array.
Syntax: array_name.append(element)
3. Remove Elements:
There are two methods:
Remove(): the remove method is called remove by value as it takes a value as an input that has to be removed
Syntax: array.remove(value)
Pop(): pop method deletes the elements by taking the index.
Syntax: array.pop([index])
4. Slicing the array:
Array can be sliced by using indexes. Slicing creates an entirely new array. We can specify the elements we want to extract.
Syntax: array_name[start : stop]
5. Len():
It is used to find the length of the array(to specify the total number of elements in the array.
Syntax: Len(array_name)
6. Updating Array Elements:
To update an element, you specify the index of the element you want to change, use the assignment operator (=) and provide the new value.
my_array = [10, 20, 30, 40]
print(f"Initial Array: {my_array}") # [10, 20, 30, 40]
# Access element at index 1
print(f" {my_array[1]}") # 20
# Update
my_array[1] = 99
print(f" After Update: {my_array}") # [10, 99, 30, 40]
# Insert()
my_array.insert(2, 25)
print(f" After Insert : {my_array}") # [10, 99, 25, 30, 40]
# Append()
my_array.append(50)
print(f" After Append (50): {my_array}") # [10, 99, 25, 30, 40, 50]
# Slicing
sliced_array = my_array[1:3]
print(f" {sliced_array}") # [99, 25]
print(f"Original Array: {my_array}") # [10, 99, 25, 30, 40, 50]
# Remove()
my_array.remove(25)
print(f" {my_array}") # [10, 99, 30, 40, 50]
# Pop()
removed_item = my_array.pop(0)
print(f" {removed_item}") # 10
print(f" {my_array}") # [99, 30, 40, 50]
# len()
array_length = len(my_array)
print(f" {array_length}") # 4
Using NumPy Arrays (The Professional Way)
A NumPy array is the foundational data structure of the NumPy library, which is the cornerstone of computing in Python. It is a container that stores elements of the same type and size in a contiguous block of memory.
NumPy array benefits
- Extremely fast
- Supports multi-dimensional arrays
- Vast library of mathematical functions
- Ideal for AI, ML, scientific computing
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
# [1 2 3 4]
FAQ (Frequently Asked Questions)
1) How to define an array in Python?
In Python you can define an array using the built-in array module or using NumPy arrays.
Using the built-in array module:
import array as arr
numbers = arr.array(‘i’, [10, 20, 30, 40])
print(numbers)
Using NumPy:
import numpy as np
numbers = np.array([10, 20, 30, 40])
2) How to reverse an array in Python?
There are several methods for it but the easiest method is to use the slicing
original_array = [10, 20, 30, 40, 50]
reversed_array = original_array[::-1]
print(f” {reversed_array}”) # [50, 40, 30, 20, 10]
3) How do you access elements in Python array?
You access elements using indexes, starting from 0.
print(numbers[0]) # First element
print(numbers[-1]) # Last element
4) How do you add an element to an array?
You can either use the .append() or .insert():
numbers.append(50) # Add at the end
numbers.insert(2, 25) # Insert at index 2
5) How do you remove an element from an array?
To remove an element from an array you can use .remove() or .pop():
array.remove(50) # Removes the first occurrence of 20
array.pop() # To Remove the last element