Arrays

 

Arrays

In Python Array is not an inbuilt Data class like in Other Languages like C,C++ or Java. Unlike a List or Tuple which can contain mixed Data Elements, All the Elements in the Array should be of the same Data type.

 

The current version of Python can have only Number Data type. But Python of version 2 had Character data type also. To use Array Data Class, we have to import Array Module.

 

Syntax

import array

or

from array import array

or

from array import array as ar

 

Array

 

Depending on how we imported the array method, the usage syntax varies.

 

Syntax

 

<arr_var>=array.array(<type code>,[<List of Values>])   or

 

<arr_var>=array (<type code>,[<List of Values>])                             or

 

<arr_var>=ar(<type code>,[<List of Values>])   or

 

Where type code is a character which denotes the data type that is to be stored in the array.

 

          i or I  - integer

          f or F - float

          h or H - unsigned integer

          u or U - unicode

          b or B - byte

          l or L  -  Long

 

 The Array can be accessed as a whole or as an individual element. For entire array, refer the array variable directly. For individual element access use the Index values, index as usual will be from 0 to size -1.

 

Methods

 

append

Appends to the end of an Array

extend

Appends multiple values to the end of an Array

insert

Inserts a value at the desired position

fromlist

Appends value from the List into the Array

remove

Element specified is removed

pop

Removes the last element

len

Returns the size of the array

count

Count of number of occurances of a value

tolist

Converts the Array to Python List

reverse

Reverse the values in the array

 

Example

 

from array import array as ar

a=ar('i',[1,2,3,4,5])

print("The integer array is",a)

print("The 3rd Element of the array is",a[2])

 

b=ar('f',[2.3,4.5,6.7])

print("The float number array is",b)

print("The 2nd Element of the array is",b[1])

 

Output

('The integer array is', array('i', [1, 2, 3, 4, 5]))

('The 3rd Element of the array is', 3)

('The float number array is', array('f', [2.299999952316284, 4.5, 6.699999809265137]))

('The 2nd Element of the array is', 4.5)

 

#append

from array import array as ar

a=ar('i',[1,2,3,4,5])

a.append(6)

print("The modified array a is",a)

 

Output

('The modified array a is', array('i', [1, 2, 3, 4, 5, 6]))

 

#insert

from array import array as ar

a=ar('i',[1,2,3,4,5])

a.insert(0,0)

print("The modified array a is",a)

print("The 3rd Element of the array is ",a[2])

 

Output

('The modified array a is', array('i', [0, 1, 2, 3, 4, 5]))

('The 3rd Element of the array is ', 2)

 

#extend

from array import array as ar

a=ar('i',[1,2,3,4,5])

a.extend([7,8,9,10])

print("The modified array a is",a)

 

Output

('The modified array a is', array('i', [1, 2, 3, 4, 5, 7, 8, 9, 10]))

 

#fromlist

 

from array import array as ar

a=ar('i',[1,2,3,4,5])

c=[11,12,13]

a.fromlist(c)

print("The modified array a is",a)

 

Output

('The modified array a is', array('i', [1, 2, 3, 4, 5, 11, 12, 13]))

 

#remove

 

from array import array as ar

a=ar('i',[1,2,3,4,5,11,12,13])

a.remove(13)

print("The modified array a is",a)

 

Output

('The modified array a is', array('i', [1, 2, 3, 4, 5, 11, 12]))

 

#pop

 

>>> print(a.pop())

12

>>> print("The modified array a is",a)

('The modified array a is', array('i', [1, 2, 3, 4, 5, 11]))

 

#len

 

>>> print("The number of elements in the Array a is",len(a))

('The number of elements in the Array a is', 6)

 

#tolist

>>> d=a.tolist()

>>> print("The List generated is",d,"and type of d is",type(d))

('The List generated is', [1, 2, 3, 4, 5, 11], 'and type of d is', <type 'list'>)

 

#reverse

from array import array as ar

a=[0,1,2,3,4,5,11,12,13]

print("Elements in reverse order is ",a.reverse())

 

Output

Elements in reverse order is: [13,12,11,5,4,3,2,1,0]

 

List vs Tuple

 

Similarities

 

]      They are both used to store collection of data

]      They are both heterogenous data types means that you can store any kind of data type

]      They are both ordered means the order in which you put the items are kept.

]      They are both sequential data types so you can iterate over the items contained.

]      Items of both types can be accessed by an integer index operator, provided in square brackets, [index]

 

 

Differences

 

JList is mutable, whereas Tuple is immutable. This means that tuples cannot be changed while the lists can be modified.

JTuple is more Memory efficient as the size of Tuple is not going to change once created as it is not modifiable. Whereas, if the List gets extended the memory too has to be extended. Also memory requirement for identical List and Tuple Objects differ. List consumes more Memory. Try it yourself by using sys.get-sizeof(<vn>) function.

JWhen it comes to Time efficiency Tuple is more efficient than List in search operation.

JWhile Lists have several in built Methods , Tuple doesn't have them.

 

 

So when to use Tuple over List?

 

If you are sure, that the Data to be used is not going to be changed throughout the Runtime, use Tuple over List. If you are unsure then use List.

No comments:

Post a Comment