How to use array in python | learn array in python

What is an Array?

An Array is basically a data structure that can hold more than one value at a time. It is a collection or ordered series of elements of the same type.

In simple words the Array store the value more than 1.

Basic Structure of Array

Array



The above image shows the example of the array, The 'a' store the value of the element of the array.  And in the array indexing started from the zero that is given in the above array. Therefore the 1 is store in the zero indexing, so it is clear that the indexing in the array starts from zero.

Therefore a[0] hold1, a[1] hold 2, a[2] hold 3, a[3] hold 4, a[4] holds 5 and a[5] holds 6

The conclusion is that the length of the array is n and the index value is the n-1

What does an ordered series of elements mean?

Ordered series of an element means that every element is present it's a particular address and this address is specified by its indexed value that was already discussed above.

This Array is also the same in a different language?


Yes, Array is the same in all the programming languages, the concept is always the same in all the programming languages but the syntax of the Array is different. 

Difference between Python list and Array?


This is the common question that asked by many people because the list and the Array are similar that's why it confuses many people.

Python Arrays and lists have the same method or way of storing data in it.

The array takes only the single data type elements but the list can have any type of data. 
For example, if I have an array of the float value and all the value in the Array will be only the float value nothing other value, and on the other hand, the list store the value of the integer, float, strings.

When we perform an operation like slicing or looping it will be similar to both. But when we do different operations like when we want to multiply the array by 2 then it's easy but it's difficult to multiple the 2 in the lists.

How to create an Array in python?

Array in python can be created after importing the array module.

1. Without Alias
import array

Example of the array

 
  import array
  
  
a = array.array( 'i', [1, 2, 3, 4, 5, 6] )
print(a)

Output
array( 'i', [1, 2, 3, 4, 5, 6] )

In the above code, we create an array a. And we can user array.array and we use i as an integer and the first array is the name of the module and the second array is an array constructor. Then we give the type code i.

2. Using Alias
import array as arr

import array as arr
a = arr.array( 'i', [1, 2, 3, 4, 5, 6, 7] )
print(a)
Output:
array( 'i', [1, 2, 3, 4, 5, 6, 7] )

3. Using *
from array import *

from array import *
a = array( 'i', [11, 2, 3, 4, 51, 6] )
print(a)

Output:
array( 'i', [11, 2, 3, 4, 51, 6] )

Accessing Array Elements

To access the array elements we want to use an ay indexing value that hold the unique elements. And we already discussed above that the indexing starts from zero to n-1. And the negative indexing also exists its starts from the right to left and positive start from the left to right.

To access the elements we have to use a[ index value ]. a is stand for the name of the array and in the brackets that we have to put the index value that we want to show or access.

Example:

from array import *
a = array( 'i', [11, 2, 3, 4, 51, 6] )
print(a[1])

Output:
2

Negative accessing

from array import *
a = array( 'i', [1, 2, 3, 4, 5, 6] )
print(a[-1])

Output:
6

Learn Basic Array Operations

Array are changeable therefore we can add or remove the array from it by doing some operation.

Finding the length of an Array

The length of an Array is actually the elements are present in an Array, In simple words the number of elements in an array.

To find the length of an array we want to use len() function. And the len() function uses one parameter that's the name of the array. For example len(a).

Example
from array import *
a = array( 'i', [1, 2, 3, 4, 5, 6, 7, 8,  9, 10, 11, 12, 13] )
print( len(a) )

Output:
13

Adding elements in an Array

To add the elements in an Array we use three function append(), extend(), insert().
append() function is used when you want to add single elements at the end of the array.
extend() function is used when you want to add more than one element at the end of the array.
insert() function is used when you want to add elements at any specific position in an array.

Example of all three function

import array as arr
a = arr.array ( 'd',[ 1.0, 2.2, 3.4 ] )
a.append( 4.4 )
print( "append", a )

b = arr.array ( 'd',[ 1.0, 2.2, 3.4 ] )
b.extend( [4.4, 5.6, 7.8 ])
print( "extend", b )

c = arr.array ( 'd',[ 1.0, 2.2, 3.4 ] )
c.insert( 1, 9.8 )
print( "insert", c )

Output

append array('d', [1.0, 2.2, 3.4, 4.4])
extend array('d', [1.0, 2.2, 3.4, 4.4, 5.6, 7.8])
insert array('d', [1.0, 9.8, 2.2, 3.4])

Please note that when you use the extend function you want to use square brackets if you do not use then you get an error, so make sure you want to use a square bracket and in the insert function first parameter is for the position and the other is for the elements.

Removing elements from an Array

To remove the elements from the array you want to use two functions, pop() function and remove().
pop() function is used when you want to remove an element and return it.
The parameter that is used in the pop function is the index value that you want to remove it and if you not give any parameter then it removes the last element from the array.

remove() function is used when you want to remove a specific value without return it.
remove function used only one parameter that's elements to remove.

Example

import array as arr
a = arr.array('i', [1, 2, 3, 4, 5 ])
a.pop()
print(a)

b = arr.array('i', [1, 2, 3, 4, 5 ])
b.pop(2)
print(b)

c = arr.array('i', [1, 2, 3, 4, 5 ])
c.remove(2)
print(c)

Output
array('i', [1, 2, 3, 4])
array('i', [1, 2, 4, 5])
array('i', [1, 3, 4, 5])

Array Concatenation

Array concatenation means to join the array with each other. And to join the different array we use + to join the arrays.

Example

import array as arr
a = arr.array('i', [1, 2, 3, 4, 5 ])
b = arr.array('i', [6, 7, 8, 9, 10 ])
c = arr.array( 'i' )
c = a+b
print("New array",c)

Output
New array array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Note that all the array have the same type. And array was join end to end.

Slicing an Array

Slicing is used to fetch elements from the array. And an array can be slicing from : symbol. This returns the range of the elements that are specified by the index value.

Example

import array as arr
a = arr.array('i', [6, 7, 8, 9, 10, 11, 12, 13 ])
print(a[0:4])

Output
array('i', [6, 7, 8, 9])

To reverse the array we can use a[::-1], Your work is to do this at own and try this that they can work or not.

Looping through an Array

There are two types of the loop for loop and while loop
for loop iterates over an item of an array specified number of times. And while loop iterates over the elements until a certain condition are met.

Example of the for loop

import array as arr
a = arr.array('i', [6, 7, 8, 9, 10, 11, 12, 13 ])
for i in a:
    print(i)

Output
6
7
8
9
10
11
12
13

Example of the while loop

import array as arr
a = arr.array('i', [6, 7, 8, 9, 10, 11, 12, 13 ])
z = 0
while z<len(a):
    print(a[z])
    z+=1

Output
6
7
8
9
10
11
12
13

Conclusion

We have clear our some doubt and we also understand the various operation of an Array and we learn the array and we also do many examples related to Array.

Next Post Previous Post
2 Comments
  • Python Training Expert
    Python Training Expert January 5, 2021 at 3:58 PM

    I checked Your Post and it is really one of the Best Post for Us & Knowledgeable Otherwise if anyone Want Python, Data Science & Machine Learning Course So You Can Contact Us-+91-9310332343
    Online Python Training Course in Delhi
    Online Python Training Institute in Delhi

  • Python
    Python January 18, 2021 at 5:35 PM

    Thank you for sharing.
    Data Science Online Training
    Python Online Training
    Salesforce Online Training

Add Comment
comment url