Python Arrays

Arrays are a fundamental data structure in many programming languages including Python. They allow to the store and manage collections of items efficiently. This article provides a comprehensive guide to understanding creating and manipulating arrays in Python.

What is an Array?

A group of objects kept in memory at close distances is called an array. By grouping similar items together it will be simpler to compute each element's position by just adding an offset to the base value.

Creating Arrays in Python

1. Using Lists:

The Lists are the most straightforward way to create arrays in Python. We can create a list using square brackets [] and separate items with commas.

my_list = [1, 2, 3, 4, 5]
print(my_list) 

Output: 

[1, 2, 3, 4, 5]


2. Using the array Module:

The array module provides a more memory-efficient way to create arrays. Unlike lists, arrays created using the array module can only store items of the same type.

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])

The first argument to the array function specifies the type of the elements. For example, 'i' stands for signed integers and 'f' stands for floating-point numbers.

Array Operations

1. Accessing Elements:

We can access elements in an array using their index. Indexing starts from the 0 in Python.

print(my_array[0]) 
print(my_array[2]) 

Output: 

1

3

2. Modifying Elements:

We can modify elements in an array by assigning the new values to specific indices.

my_array[0] = 10
print(my_array) 

Output: 

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

3. Appending Elements:

Use the append method to add elements to the end of the array.

my_array.append(6)
print(my_array) 

Output: 

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

4. Inserting Elements:

Use the insert method to insert an element at a specific position.

my_array.insert(120)
print(my_array) 

Output

array('i', [10, 20, 2, 3, 4, 5, 6])

5. Removing Elements:

Use the remove method to remove the first occurrence of a specific element.

my_array.remove(20)
print(my_array)

Output: 

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

6. Popping Elements:

Use the pop method to remove and return the last element of the array.

last_element = my_array.pop()
print(last_element) 
print(my_array)

Output: 
6
array('i', [10, 2, 3, 4, 5])

Array Slicing

The Slicing allows the create a new array by extracting a subset of the elements from an existing array. The syntax for slicing is array[start:stop:step].

sliced_array = my_array[1:4] # Extract elements from index 1 to 3
print(sliced_array)

Output:
array('i', [2, 3, 4])

Multidimensional Arrays

While the array module only supports one-dimensional arrays we can create multidimensional arrays using the nested lists or by using libraries like NumPy.

1. Using Nested Lists:

matrix = [
[1, 2, 3], 
[4, 5, 6], 
[7, 8, 9] ] 
print(matrix[0][1])

Output: 2

2. Using NumPy:

The NumPy is a powerful library for numerical computations that supports multidimensional arrays.

import numpy as np
matrix = np.array([ 
[1, 2, 3], 
[4, 5, 6], 
[7, 8, 9] ]) 
print(matrix[0, 1])

Output: 2

Post a Comment

0 Comments