Python Arrays – How to use arrays in Python

Python Arrays

An array is a type of variable that can hold one or multiple values in a single variable.

Arrays in Python are similar to list, that we learnt in last chapter. However the difference is a list can hold multi type elements whereas arrays can hold only single type.

Unlike other programming languages, like PHP arrays in Python are not among the core data types like strings, integers etc. In order to use arrays in your programs you have to import built-in standard module as follows:

from array import array

Creating an array

Here is an example of creating an array:

array_name = array(typecode[,array values])

In order to define an array, as mentioned earlier we have to import array module. Then array name and its type-code needs to be specified.

Following type codes are supported with types:

Type codeC TypePython TypeMinimum size in bytes
‘b’signed charInt1
‘B’unsigned charInt1
‘u’Py_UNICODECharacter2
‘h’signed shortInt2
‘H’unsigned shortInt2
‘i’signed intInt2
‘I’unsigned intInt2
‘l’signed longInt4
‘L’unsigned longInt4
‘q’signed longInt8
‘Q’unsigned longInt8
‘f’FloatFloat4
‘d’DoubleFloat8

Array starts with 0 index.

Running example of creating an Array



Output will be:

current array item: 1

current array item: 2

current array item: 3

current array item: 4

current array item: 5

A few useful array methods

Remove method of array

Below example shows how to use remove method of array to remove array element(s).



Output will be:

Array before remove: 1

Array before remove: 2

Array before remove: 3

Array before remove: 4

Array before remove: 5

##########################

array after remove 1

array after remove 2

array after remove 4

array after remove 5

Using pop method to remove element

Pop method removes element by index, starting from 0. If no argument is passed it will remove last element from array.

Example



Output will be:

Array before remove: array(‘b’, [1, 2, 3, 4, 5])

##########################

array after remove array(‘b’, [2, 3, 4, 5])

Adding elements to existing array

Python has a few methods to add elements to an array.

Using append method

Append method adds an element to the end of an existing array. See example below:



Output will be:

Array before adding item: array(‘b’, [1, 2, 3, 4, 5])

##########################

array after adding item array(‘b’, [1, 2, 3, 4, 5, 8])

Using Insert() method

Insert method is used to add elements at specified position in an existing array. It takes two arguments, one that specifies element  position to be added and other specifies array element.

Syntax

Insert(position, element)

Example of using insert method



Output will be:

Array before adding item: array(‘b’, [1, 2, 3, 4, 5])

##########################

array after adding item: array(‘b’, [8, 1, 2, 3, 4, 5])

Adding a list to an existing array using fromlist() method

fromlist() method can be used to add a list to an existing array. See example how to use this:



Output will be

Array before adding list: array(‘b’, [1, 2, 3, 4, 5])

Array after adding list: array(‘b’, [1, 2, 3, 4, 5, 8, 9, 10])

 

Length of an array

To know how many items are in array, len(array_name) function can be used as follows:



Output will be:

Length of array: 5

Was this article helpful?

Related Articles

Leave A Comment?