Example HTML page

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:

>>> import array
>>> dir(array)
['ArrayType', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_array_reconstructor', 'array', 'typecodes']

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:

###creating an array and appended two lists into it as elements
>>> arr_1=[[5,3,1],[4,2,7]]
>>> for a in arr_1:
	print(a)

	
[5, 3, 1]
[4, 2, 7]


###array with some elements
>>> arr_2=[9,9,8,5,3,2,7,1,9,9]
>>> for m in arr_2:
	print(m)

	
9
9
8
5
3
2
7
1
9
9

###Array with different data types
>>> Array=["Hi", 2 , "Sudhakar", 2.456]
>>> for x in range(len(Array)):
	print(Array[x])

	
Hi
2
Sudhakar
2.456


###Traversing on 2d array
>>> Two_d=[[ "Sudhakar", 2.456,"abc"],[9,8,7,6]]
>>> for y in range(len(Two_d)):
	print(Two_d[y])

	
['Sudhakar', 2.456, 'abc']
[9, 8, 7, 6]


###Traversing on 3d array
>>> Three_d=[[3],["Dimensional"],[345,421,5]]
>>> for z in range(len(Three_d)):
	print(Three_d[z])

	
[3]
['Dimensional']
[345, 421, 5]

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.

import array

#Initialization of an array
arr = array.array('i', [20, 25, 30,35,40])  
print ("The newly created array is : ",end="") 
for i in range (0,3): 
    print (arr[i], end=" ") 
print ("\r")

#Appending new value at the end
arr.append(4); 
print ("The appended array is : ", end="") 
for i in range (0, 4): 
    print (arr[i], end=" ")

#Inserting new value at a specific position.
arr.insert(2, 5)   
print("\r") 
print ("The array after insertion is : ", end="") 
for i in range (0, 5): 
    print (arr[i], end=" ")

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.

import array 
arr= array.array('i',[4,8,2,7,1,8,2])  
print ("The newly created array is : ",end="") 
for i in range (0,5): 
    print (arr[i],end=" ") 
print ("\r") 

# using pop() to remove element at 3rd position 
print ("The popped element is : ",end="") 
print (arr.pop(3)); 
  
# printing array after popping 
print ("The array after popping is : ",end="") 
for i in range (0,4): 
    print (arr[i],end=" ") 
print("\r") 
  
# using remove( ) to remove 1st occurrence of 8 
arr.remove(8) 
  
# printing array after removing 
print ("The array after removing is : ",end="") 
for i in range (0,3): 
    print (arr[i],end=" ")

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.

import array 
arr= array.array('i',[1, 2, 3, 1, 2, 5])  
print ("The newly created array is : ",end="") 
for i in range (0,6): 
    print (arr[i],end=" ") 
print ("\r") 
  
# using index() to print index of 1st occurrenece of 2 
print ("The index of 1st occurrence of 2 is : ",end="") 
print (arr.index(2)) 
  
#using reverse() to reverse the array 
arr.reverse() 
  
# printing array after reversing 
print ("The array after reversing is : ",end="") 
for i in range (0,6): 
    print (arr[i],end=" ")

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.

import array
my_array=array.array("i",(9,5,1,8,6))
print("new_array: "+str(my_array))
list=array.array.tolist(my_array)
print("An array is converted into a list as: ",list)

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.

marks=["Sudhakar",85,90.7]
x=marks[1]+marks[2]
print("Total score of Sudhakar is: ",marks[1], "+" ,marks[2],"=",x)

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’.

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest