# Array in Python

In programming aspects, Array is collection of elements/objects and is the most efficient data structure to store and access elements. The elements in an array can be referred by keeping the square brackets[ ]. Python doesn’t have a native **array** data structure, but we can use lists instead of arrays. In python the lists are implemented as dynamic arrays internally. This means lists allow elements to be added or removed and they will automatically adjust the backing store that holds these elements by allocating or releasing memory. However there is a module in Python called “array” that supports array of numerical values.

**Importing an array module: **

**n-dimensional array:**

**Declaration of 2 dimensional array:**

The two dimensional array in Python can be declared as follows:

>>> array_2d=[[1,3,5,7],[2,4,6,8]]
>>> array_2d [[1, 3, 5, 7], [2, 4, 6, 8]] >>> array_2d[1] [2, 4, 6, 8] >>> array_2d[0] [1, 3, 5, 7] >>> array_2d[1][-1] 8 >>> array_2d[1][3] 8 >>> array_2d[1][0] 2 >>> array_2d[0][2] 5 |

** ****Declaration of 3 dimensional array/Multi-dimensional array:**

The three dimensional array in Python can be declared as follows.

>>> array_3d=[[“VMS”,26,5.16],[“Sudhakar”,52,1.6],[“C++”,34,1.2]]
>>> array_3d [[‘VMS’, 26, 5.16], [‘Sudhakar’, 52, 1.6], [‘C++’, 34, 1.2]] >>> array_3d[2] [‘C++’, 34, 1.2] >>> array_3d[0][0] ‘VMS’ >>> array_3d[1][0] ‘Sudhakar’ >>> array_3d[0][1] 26 >>> array_3d[2][0] ‘C++’ |

** ****Iterations on array:**

In order to traverse the elements in array we use** ‘for’** loop. The following is a simple example of array using for loop:

**Operations on array:**

The following are some of the operations that are being performed on array.

**array(data type , value list) – **This function helps to create an array with data type and value list specified in its arguments.

**Ex**

>>> arr=array.array(‘i’,[1,2,3])
>>> arr array(‘i’, [1, 2, 3]) |

In the above example ‘i’ is an integer data type and [1,2,3] is the value list.It represents the creation of an array.

To define the **type codes** of the array values there exist some types codes in python. The below is the list of available **typecodes**.

‘ b’ → Represents signed integer of size 1 byte
‘B’ → Represents unsigned integer of size 1 byte ‘c’ → Represents character of size 1 byte ‘u’→ Represents Unicode character of size 2 bytes ‘h’ → Represents signed integer of size 2 bytes ‘H’ → Represents unsigned integer of size 2 bytes ‘i’→ Represents signed integer of size 2 bytes ‘I’ → Represents unsigned integer of size 2 bytes ‘w’ → Represents Unicode character of size 4 bytes ‘l’→ Represents signed integer of size 4 bytes ‘L’ → Represents unsigned integer of size 4 bytes ‘f’ → Represents floating point of size 4 bytes ‘d’ → Represents floating point of size 8 bytes |

After creating an array we need to append values in to it. For this there is a function called append( ).

**append( )** – This function appends new value at the end of that array. For the same above example we are appending a value.

**Ex**

>>> arr.append(6)
>>> arr array(‘i’, [1, 2, 3, 6]) |

We can also append an array into another array as shown in below:

>>> arr_1=[[5,3,1],[4,2,7]]
>>> new_arr=[5.7,9.1] >>> arr_1.append(new_arr) >>> arr_1 [[5, 3, 1], [4, 2, 7], [5.7, 9.1]] |

If you want to add the value at a specific position then you can use insert function.

**Insert(i,x)** – This function inserts the new value at a particular position. Where ‘i’ refers index position and ‘x’ is new value which we want to insert.

**Ex**

>>> arr
array(‘i’, [1, 2, 3, 6]) >>> arr.insert(0,16) >>> arr array(‘i’, [16, 1, 2, 3, 6]) >>> arr.insert(4,7) >>> arr array(‘i’, [16, 1, 2, 3, 7, 6]) |

Example program which describes **append( )** and **insert( )** functions.

**Output**

The newly created array is : 20 25 30
The appended array is : 20 25 30 35 The array after insertion is : 20 25 5 30 35 |

** Pop( )** — This function removes the element at the position specified in its argument.

**Ex**

>>> arr=array.array(‘i’, [16, 1, 2, 3, 7, 6])
>>> arr array(‘i’, [16, 1, 2, 3, 7, 6]) >>> arr.pop(0) 16 >>> arr array(‘i’, [1, 2, 3, 7, 6]) >>> arr.pop(1) 2 >>> arr array(‘i’, [1, 3, 7, 6]) |

**Remove( )** — This function removes the first occurrence value specified in its argument.

**Ex**

>>> arr=array.array(‘i’, [4,8,5,9,3,1,5])
>>> arr array(‘i’, [4, 8, 5, 9, 3, 1, 5]) >>> arr.remove(5) >>> arr array(‘i’, [4, 8, 9, 3, 1, 5]) |

In the above example we used remove ( ) to remove the first occurrence of ‘5’.

The following is an example program that depicts the **pop( )** and **remove( )** functions.

**Output**

The newly created array is : 4 8 2 7 1
The popped element is : 7 The array after popping is : 4 8 2 1 The array after removing is : 4 2 1 |

**index( )** — This function returns the index position of the first occurrence of value specified in its argument.

**Ex**

>>> arr=array.array(‘i’,[12,23,45,1,23,8,7])
>>> arr.index(23) 1 |

** Reverse( )** –this function reverses the array.

**Ex**

>>> arr.reverse( )
>>> arr array(‘i’, [7, 8, 23, 1, 45, 23, 12]) |

The following is an example program that depicts the** index( )** and **reverse( )** functions.

**Output**

The newly created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1 The array after reversing is : 5 2 1 3 2 1 |

** ****Example programs on array in Python:**

**1. An example Python program to convert an array to an ordinary list with the same items.**

**Output**

new_array: array(‘i’, [9, 5, 1, 8, 6])
An array is converted into a list as: [9, 5, 1, 8, 6] |

In the above example we use **tolist( )** method to convert an array to an ordinary list.

**2.An example Python program that illustrates the declaration of different data types in the same array.**

**Output**

Total score of Sudhakar is: 85 + 90.7 = 175.7 |

In the above example you can observe that the array ‘marks’ have three different data types. Here marks[1] and marks[2] represents the index positions of the elements in ‘marks’.