Array in Python


An Array is a collection of similar data items stored in contiguously on memory.

We can use List as array.

The difference between the array and List is, we can not restrict List to store the type of element.

for example :

a = [1, 2.5, 'Python']

As we can see in list there may be any type's element.

In python, we can create array by using array module.

Creating an Array :

Array in python can be created by importing array module.

import array as ar

Once we have imported the array module, we can declare an array. Here is how we do it:

arrayName = array(typecode, [Initializers])

In the above declaration, 'typecode' lets python know the type of array and 'Initializers' are the values with which array is initialized.

for example :


import array as ar
myArray = ar.array('i',[1, 2, 3, 4, 5])
                

In the example above, typecode used is 'i'. This typecode represents signed integer whose size is 2 bytes.

Commonly used type codes are :

  Type Code      Represents   Python Type   Size in Bytes   
'b'Signed integerint1
'B'unsigned integerint1
'u'unicode characterUnicode Character2
'h'signed short int2
'H'unsigned shortint2
'i'signed integerint2
'I'unsigned integerint2
'l'signed longint4
'L'unsigned longint4
'q'signed long longint8
'Q'unsigned long longint8
'f'floatfloat4
'd'doublefloat8

Since List and array are related to each other in terms of operation, you should know how to use Python List .


1. Creation of array with 5 elements

>>> import array as ar
>>> myArray = ar.array('i',[1, 2, 3, 4, 5])
                

2. Access array elements :

We use indices to access array elements.It also support negative index.

>>> print(myArray[0])
1
>>> print(myArray[3])
4
>>> print("Last Element",myArray[-1])
Last Element 5
                

3. Slicing an array :

It is similar to list.

>>> print(myArray[:])
array('i', [1, 2, 3, 4, 5])
>>> print(myArray[1:4])
array('i', [2, 3, 4])          
                

4. Append a new element :

>>> myArray.append(20)
>>> print(myArray)
array('i', [1, 2, 3, 4, 5, 20])
                

5. Insert a new value :

>>> myArray.insert(0,10)
>>> print(myArray)
array('i', [10, 1, 2, 3, 4, 5, 20])
                

6. Extend array :

>>> new_array = ar.array('i',[10,20,30,40])
>>> myArray.extend(new_array)
>>> print(myArray)
array('i', [10, 1, 2, 3, 4, 5, 20, 10, 20, 30, 40])
>>>
                

7. Add items from List to array :

>>> myArray = ar.array('i',[1,2,3,4,5])
>>> myList = [10,20,30,40,50]
>>> myArray.fromlist(myList)
>>> print(myArray)
array('i', [1, 2, 3, 4, 5, 10, 20, 30, 40, 50])
                

8. Remove an array element :

>>> myArray = ar.array('i',[1,2,3,4,5])
>>> myArray.remove(3)
>>> print(myArray)
array('i', [1, 2, 4, 5])
>>> 

9. Pop an element from array :

>>> myArray = ar.array('i',[1,2,3,4,5])
>>> myArray.pop()
5
>>> 

10. Get the element's index :

>>> myArray = ar.array('i',[1,2,3,4,5])
>>> myArray.index(4)
3
>>> myArray.index(5)
4
>>> 

11. Reverse an array :

>>> myArray = ar.array('i',[1,2,3,4,5])
>>> myArray.reverse()
>>> print(myArray)
array('i', [5, 4, 3, 2, 1])
>>>  

12. Get the occurrence of an element :

>>> myArray = ar.array('i',[1,1,1,2,3,4,5])
>>> myArray.count(1)
3
>>>  

13. Convert array to a List :

>>> myArray = ar.array('i',[1,1,1,2,3,4,5])
>>> myList = myArray.tolist()
>>> print(myList)
[1, 1, 1, 2, 3, 4, 5]
>>>  

Next chapter is Python Functions






 

Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved