Quick Reach
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 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 |
Array starts with 0 index.
Running example of creating an Array
1
2
3
4
5
6
7
|
from array import array
Intarr=array(‘b’,[1,2,3,4,5])
for i in Intarr:
print(“current array item:”, i)
|
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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from array import array
Intarr=array(‘b’,[1,2,3,4,5])
for i in Intarr:
print(“Array before remove:”, i)
print (“##########################”)
Intarr.remove(3)
for i in Intarr:
print(“array after remove”, i)
|
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
1
2
3
4
5
6
7
8
9
10
11
|
from array import array
Intarr=array(‘b’,[1,2,3,4,5])
print(“Array before remove:”, Intarr)
print (“##########################”)
Intarr.pop(0)
print(“array after remove”, Intarr)
|
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:
1
2
3
4
5
6
7
8
9
10
11
|
from array import array
Intarr=array(‘b’,[1,2,3,4,5])
print(“Array before adding item:”, Intarr)
print (“##########################”)
Intarr.append(8)
print(“array after adding item”, Intarr)
|
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
1
2
3
4
5
6
7
8
9
10
11
|
from array import array
Intarr=array(‘b’,[1,2,3,4,5])
print(“Array before adding item:”, Intarr)
print (“##########################”)
Intarr.insert(0,8)
print(“array after adding item”, Intarr)
|
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:
1
2
3
4
5
6
7
8
9
10
11
|
from array import array
Intarr=array(‘b’,[1,2,3,4,5])
print(“Array before adding list:”, Intarr)
list_a= [8,9,10]
Intarr.fromlist(list_a)
print(“Array after adding list:”, Intarr)
|
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:
1
2
3
4
5
|
from array import array
Intarr=array(‘b’,[1,2,3,4,5])
print(“Length of array:”, len(Intarr))
|
Output will be:
Length of array: 5
Leave A Comment?