Arrays in Python

Python tutorial · PySpark.in

Arrays in Python

In Python, an array is a data structure that stores multiple values of the same data type in a single variable.

Importing array

Before using arrays, you must import them from the array module:

from array import array

 Creating an Array

Each array needs:

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3print(numbers)
▶ Output will appear here.

Accessing Array Elements

Array elements are accessed using indexing, just like lists (starting from index 0).

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3print(numbers[0]) # 10 (first element)
4print(numbers[2]) # 30 (third element)
▶ Output will appear here.

Slicing Arrays

You can extract a part of an array using slicing:

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3print(numbers[1:4]) # array('i', [20, 30, 40])
▶ Output will appear here.

This returns elements from index 1 to 3 (end index is excluded).

Looping Through Arrays

You can iterate over array elements in two common ways:

Using a for loop(direct items)

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3
4for num in numbers:
5 print(num)
▶ Output will appear here.

Using indexing

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3for i in range(len(numbers)):
4 print(numbers[i])
▶ Output will appear here.

Array Operations

Append an element at the end

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3numbers.append(60)
4print(numbers)
▶ Output will appear here.

Insert at a specific index

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3numbers.insert(2, 25) # Insert 25 at index 2
4print(numbers)
▶ Output will appear here.

Remove an element by value

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3numbers.remove(25) # Remove first occurrence of 25
4print(numbers)
▶ Output will appear here.

Extend array(add multiple elements)

PYTHON
1from array import array
2numbers = array('i', [10, 20, 30, 40, 50])
3extra = array('i', [70, 80])
4numbers.extend(extra)
5print(numbers)
▶ Output will appear here.

Array Concatenation

You can combine two arrays of the same type using +:

PYTHON
1from array import array
2a1 = array('i', [1, 2, 3])
3a2 = array('i', [4, 5, 6])
4a3 = a1 + a2
5print(a3) # array('i', [1, 2, 3, 4, 5, 6])
▶ Output will appear here.

Searching Elements

Use .index(value) to find the index of an element:

PYTHON
1from array import array
2nums = array('i', [10, 20, 30, 40])
3print(nums.index(30)) # 2
▶ Output will appear here.

Array Methods

Method

Description

Example

append(x)

Add item at end

arr.append(10)

insert(i, x)

Insert item at index i



remove(x)

Remove first occurrence of x

arr.remove(3)

pop(i)

Remove item at index i

arr.pop(2)

extend(a2)

Add all elements from another array

arr.extend(a2)

index(x)


arr.index(10)

reverse()

Reverse the array

arr.reverse()

buffer_info()

Return memory address & length

arr.buffer_info()

count(x)

Count occurrences of x

arr.count(2)

Note

Example using NumPy:

PYTHON
1import numpy as np
2arr = np.array([1, 2, 3])
3print(arr)
▶ Output will appear here.

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges