Python Array: 7 Examples to Understand Arrays and its Methods

Python Array: 7 Examples to Understand Arrays and its Methods

Python Arrays

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

The Arrays are similar to Python list, that we learned in the last chapter. However, the difference is a list can hold multi-type elements whereas the arrays can hold only single type e.g. an array of only strings or numbers.

See an example of array

An array length example

An array append example

Import Array Module

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 the 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 the array module. After that, the array name and its type-code need to be specified.

Following type codes are supported with data types:

Type code C Type Python Type Minimum size in bytes
‘b’ signed char Int 1
‘B’ unsigned char Int 1
‘u’ Py_UNICODE Character 2
‘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
‘q’ signed long Int 8
‘Q’ unsigned long Int 8
‘f’ Float Float 4
‘d’ Double Float 8

Note that, a Python Array starts at 0 index.

Example of creating an Array

In the following example, we have created a numeric array with five elements. After that, we used a for loop to iterate through array elements and displayed the elements on the screen by using the print function of Python.

Python arrays

See this example online

The output will be:

current array item: 1

current array item: 2

current array item: 3

current array item: 4

current array item: 5

Useful array methods

Following are a few useful array methods that you can use quite easily. The methods are explained with examples.

Remove method of array

The remove method of an array is used to delete an existing array element. You have to specify the element to be deleted in the remove method.

The following example shows how to use the array remove method to delete array element(s). We have created a numeric array with five elements. Then, we used the remove method to delete an element from the array.

The array is displayed before and after using the array remove method.

Python array remove

See this example online

The output:

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

The Pop method removes an element by index number, starting from 0. If no argument is passed, it will remove the last element in the Python array. See the following example to learn how it works.

See this example online

The output of above code:

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.

Array append method

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

Python array append

See this example online

The Output:

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

The insert method of array Python is used to add elements at the specified position in an existing array. It takes two arguments; one that specifies the element position and other specifies the array element.

Syntax

Insert(position, element)

Example of using insert method

See this example online

The output of code:

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

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

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

Add a list to an array using fromlist()

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

Python array list

See this example online

Output

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

How to know the array length?

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

See this example online

The Output:

Length of array: 5

 

Useful Reading: Python Dict | Python Set