sampleqa.in tutorials, Python Programming  tutorial

Python Arrays


     Python Arrays are collection of data of same type. Arrays are also sequence type in Python. Compared to other Programming languages, python Arrays supports , Character, Integer and Floating point arrays only.


Note:Python Arrays are similar to Lists, except that type of objects it can hold are restricted

Python Arrays are linear data structure, and also random access data structure, Memory allocated as a continuous memory locations

It is a unbounded data structure, you can add as many elements as you can. lower bound index starts from 0, upper bound is N-1(where N is number of elements in the Array).

Create an Array

Synatx:
		import array
		
		array.array(typecode, [values......]);
	
Typecodes(copied from python.org)
   Type code   C Type             Minimum size in bytes
     |      'b'         signed integer     1
     |      'B'         unsigned integer   1
     |      'u'         Unicode character  2 (see note)
     |      'h'         signed integer     2
     |      'H'         unsigned integer   2
     |      'i'         signed integer     2
     |      'I'         unsigned integer   2
     |      'l'         signed integer     4
     |      'L'         unsigned integer   4
     |      'q'         signed integer     8 (see note)
     |      'Q'         unsigned integer   8 (see note)
     |      'f'         floating point     4
     |      'd'         floating point     8
		import array
		
		nums = array.array('i',[10,100,1000,10000,100000]);
		
		         OR
		         
		from array import array
		nums = array('i',[10,100,1000,10000,100000]);
		
		type(nums)
		<class 'array.array'>
		nums is a class of type array in module array.
	

    In above example , we have created an array of type signed integer,by specifying typecode 'i' with initial values,10,100,1000,10000,100000

Accessing Array elements

   Like List, Array elements are also accessed by index, lower index starts from 0.

		>>> nums[0];nums[1];nums[2];nums[3];nums[4]
		10
		100
		1000
		10000
		100000
	

Array has 5 elements, first index is 0, last index is len(nums)-1 i.e 5-1=4

Print Array elements using loops

for-loopwhile loop
	for i in nums:
	   print(i)
	   
	
>>> i=0;
>>> while i < len(nums)-1:
      print(nums[i])
      i=i+1

Adding elements to an Array

    append , insert, extend methods used to add new elements to an array.

  • append
  •    adds single element at the end of the Array

  • insert
  •     Inserts a element before the position i

  • extend
  •    append items to the end of the array

		append method
		>>>nums
		array('i', [10, 100, 1000, 10000, 100000])
		>>> nums.append(1000000) #1000000 added to end of the array
		>>> nums
		array('i', [10, 100, 1000, 10000, 100000, 1000000])

		insert method
		
		>>> nums.insert(0,1)  #insert befor position 0
		>>> nums
		array('i', [1, 10, 100, 1000, 10000, 100000, 1000000])

		extend method

		>>> nums.extend([1,10,100])  # add multiple elements to an array
		>>> nums
		array('i', [1, 10, 100, 1000, 10000, 100000, 1000000, 1, 10, 100])
		>>> 
	

Remove elements from an Array

    pop and remove methods are used to remove/delete elemets from an array.

  • pop
  •     removes last element from the array, and returns its value. if array is empty throws IndexError Exception

  • remove
  •     removes first occurance of an element, if array is empty, then throws ValueError Exception

	>>> nums
	array('i', [10, 100, 1000, 10000, 100000])
	>>> nums.pop();nums.pop();nums.pop()
	100000
	10000
	1000
	>>> 	
        >>>nums
        array('i', [10, 100])

	>>> nums.remove(10)
	>>> nums
	array('i', [100])
	>>> 
	

Find frequency of an Element

like other built-in types, arrays also has count method, which finds repeated element count. This method is useful whenever array has repeated elements/how many times element occured in an array.

This method always return int value

Array to List,List to Array Conversion

     tolist() function converts array object into list object.
fromlist() appends list items to an array object

		>>>nums
		array('i', [10, 100, 1000, 10000, 100000])

		>>> l=nums.tolist()
		>>> l
		[10, 100, 1000, 10000, 100000]


		>>> nums.fromlist([100000,10000000])
		>>> nums
		array('i', [10, 100, 1000, 10000, 100000, 100000, 10000000])
		>>> 

		fromList appends list items to the end of an array.
	

Reverse an Array

     elements in the array can be changed reversed order
using

  • reverse method
  • slicing technique

		>>>nums.reverse()
		>>>nums
		array('i', [10000000, 100000, 100000, 10000, 1000, 100, 10])
		
		>>>nums[::-1]
		array('i', [10000000, 100000, 100000, 10000, 1000, 100, 10])
		
	

     The main difference between reverse and array silicing is, former changes order of the elements in the original array, later creates a new array object.

ADS