Arrays

Introduction

Python does not have built-in support for Arrays, but Python Lists can be used instead. You can import the array library at the beginning of the code import array as arr.
You can declare a variable Array like this:

 

import array as arr
a = arr.array('d', [1.1, 3.5, 4.5])


Here, we created an array of float type. The letter 'd' is a type code. This determines the type of the array during creation.

Commonly used type codes:

 

Code          

C Type              

Python Type       

Min bytes       

'b'

signed char

int

1

'B'

unsigned char

int

1

'h'

signed short

int

2

'H'

unsigned short

int

2

'i'

signed int

int

2

'I'

unsigned int

int

2

'l'

signed long

int

4

'L'

unsigned long

int

4

'f'

float

float

4

'd'

double

float

8

 

Accessing Array

Arrays are indexed by integers starting on position 0 and until n-1, being n the length of the Array.
Example:

Array1 = arr.array("i", [1,2,3,4,5])


Has length 5 and can be indexed like this:

 

0 1 2 3 4

 

print( Array4[0] )
print( Array4[1] )
print( Array4[2] )
print( Array4[3] )
print( Array4[4] )

 

Output: 

1
2
3
4
5


Which will print out every member of Array4.
But we can also access it with negative numbers which do it from the end to the start. Like this:

 

-5 -4 -3 -2 -1

 

print( Array4[-1] )
print( Array4[-2] )
print( Array4[-3] )
print( Array4[-4] )
print( Array4[-5] )

 

Output:

8
7
6
5
4


This will print the Array1 backward.

Or we can also use a for to iterate over the Array.

 

int = 0
while int < len(Array1):
    print(Array1[int])
    int += 1

 

Output:

8
7
6
5
4


Operations

 

Concatenation
 

We can also concatenate Arrays by adding them.

Example:

Array2 = arr.array("i", [6,7,8,9,10])
print(Array1 + Array2)  

 

Output:

array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Repetition

We can repeat the contents by x times.

Example:

print(Array1 * 3)

 

Output: 

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


Will print the Array1 tripled. 

Membership
 

There is a way to finding out if a specific value exists in a specific Array.

Example:

print( 2 in Array1)

 

Output: True


This will print out True because integer 2 indeed exists in Array1.

Iteration
 

Using the Membership operator, we can iterate over every member of an Array.
 

for member in Array1:
    print(member)

 

Output:

1
2
3
4
5


For each iteration of this loop, the temporary variable member will be assigned a value of the Array being printed out and passing on to the next value.

Length
 

There is a way to get the size of a specific Array. Using the method len().

Example:

print( len(Array1) )

 

Output: 5

 
This code will print out the size of Array1 which is 5.

Slicing
 

Arrays can be sliced to show only desired values or to iterate over desired indexes.

print(Array1 [1:3])
print(Array1 [2:])

 
First-line will print from index 1 to 3 and the second will print starting on index 2.

 
Working with Arrays

Lists are much more flexible than arrays. They can store elements of different data types including string. Also, lists are faster than arrays.

 

Function and Methods

Python Array Functions

 

Function

Description

E.g

len(Array)

It is used to calculate the length of the Array.

len( Array1 ) will return 3

max(Array)

It returns the maximum element of the Array.

max( Array1) will return 8

min(Array)

It returns the minimum element of the Array.

min( Array1) will return 1


Python Array Methods

 

Function

Description

E.g

Array.append(obj)

The object obj is added to the Array.

Array1 = arr.array("i", [1,2,3,4,5])

Array1.append(9)

Will add the integer 9 to Array1

Array.count(obj)

It returns the number of occurrences of the specified object in the Array.

print(Array1.count(3))

It will print out 1 which is the number of times 3 is in Array1.

Array.extend(seq)

Object seq is extended to the Array.

Array1.extend((7,8,9,10,11,12)

It extends Array1 adding those 5 elements.

Array.index(obj)

It returns the lowest index in the Array that object appears.

print(Array1.index(2))

It prints out 1 which is the index of 2.

Array.insert(index, obj)

The object is inserted into the Array at the specified index.

Array1.insert(2, 18)

Inserts 18 right after 2 pushing 3 and the rest to the right.

Array.pop()

It removes and returns the last object of the Array.

print(Array1.pop())

Will remove the integer 5 and print it.

Array.remove(obj)

It removes the specified object.

Array1.remove(3)

Removes the integer 3 from Array1.

Array.reverse()

It reverses the Array.

Array1.reverse()

Reverses the order of Array1.

 

Related Tutorials