In this blog we will learn

Suppose you had to store names of all the members of your group,how would you do it using python?

Storing each name in a variable like this?

m1 = ‘atufa’

m2 = ‘shireen’

..

You won’t right?

Most often programmers make use of lists in programs while programming in python, and use arrays in other famous programming languages such as Java, C, C++ etc. Well the common differences between a list and an array are

Lists can store values with different data types, but arrays can only store values with the same datatype.
You won’t have to define datatype of elements in a list, but pass the data type of elements in array as first argument
Arrays are much faster and space efficient compared to lists.

So the arrays in python are a collection of elements of the same data type in an ordered sequence, on which the operations like insertion, deletion, traversing and updating can be performed. It is one of the data structures.

Creating An Array In Python

Arrays unlike lists need to be imported in the program from the array module, and you can do it as so.

Import array

You can also import every function from array so that you wont need to use the array.array() whenever you are using arrays in your program, like this

from array import *

else,

from array import array

Since, we just need an array so we have imported the array from the array module. You can also use an alias word for array using as keyword.

The array(data type,elements) takes two positional arguments, data type such as integer,float,char,double,signed long,unsigned long etc., and the second one as the values to be stored.

For example,

arr = array.array(‘d’,[1.1,3.5,5.3])

print(arr)

Output

array(‘d’,[1.1,3.5,5.3])

Here, d is a data type for float.

Note that all the elements passed in the argument are of the same data type and cannot be of different data type.

Here is another example,

arr = array.array(‘i’,[1,2,19])

Here, i is the data type for integers. You can find all the notations for different keywords in the figure a

Type Code

Python Type

‘b’

int

‘B’

int

‘u’

Unicode character

‘h’

int

‘H’

int

‘i’

int

‘I’

int

‘l’

int

‘L’

int

‘q’

int

‘Q’

int

‘f’

float

‘d’

float

(Figure a)

Accessing elements from array

Accessing elements from an array is just as accessing elements from a list that is with indexing.Just keep in mind that indexing in programming starts at 0.

# Example program for accessing element from array


arr = array.array(‘i’,[1,2,3,4])

first_elem_arr = arr[0]

print(first_elem_arr)

Output

1

Further, the other elements can be found with the index, and the last element with an index -1, the last second with -2 index.



# Second example program for accessing element from array


arr_1 = array.array(‘i’,[1,2,3,4])

arr_2 = array.array(‘i’,arr[0],arr[-1],arr[1],arr[-2])

print(‘first,last,second,second last elements from the array 1 are: ’,arr_2)

Output

first,last,second,second last elements from the array 1 are: array(‘i’,[1,4,2,3])

To get one element at a time, we can use for or while loops to iterate over the array and print the elements.

# Example program for accessing elements with for loop


arr = array.array(‘i’,[3,5,1,2])

for i in arr:

print(i, end=’ ‘)

Output

3 5 1 2

Also, you can use the slicing method to get specific elements from the array,

The array_name[start:end:step] will return the elements in the array_name from the start index until the end index, by removing or skipping step number of elements in between.

# Example program for Slicing arrays


arr = array.array(‘i’,[1,4,5,6])

print(arr[1:3:1])

Output

array(‘i’,[1,5])

Length of an array

The length of an array can be found using the usual len() method, just as we find the length of any list.

# Example program for finding length of an array


arr = array.array(‘i’,[3,5,1,2])

print(‘length of the arr array is: ‘,arr)

Output

length of the arr array is: 4

which is equal to the size or numbers elements in the array, irrespective of the index of the last element.

Check out this Python Online Course by FITA. FITA provides a complete Python course where you will be building real-time projects like Bitly and Twitter bundled with Django, placement support and certification at an affordable price.

Adding elements to array

Elements can be added to the end of the arrays using the append() method as follows

# Example program for adding element to array


arr = array.array(‘i’,[1,3,6,12])

print(‘Array before: ‘,arr)

arr.append(3)

print(‘Array after: ’,arr)

Output

Array before: array(‘i’,[1,3,6,12])

Array after: array(‘i’,[1,3,6,12,3])

Elements can be added at a specific index using .insert() method.The insert() method takes two positional arguments , first as the index and second as the value to be inserted.The index of all the elements will be updated after an insert method.

The index of any element can be found using the iterable.index(value) method.

The index method will return -1 if the index is not found for the passed parameter.

# Example program for inserting element to array at specified index


arr = array.array(‘i’,[1,3,6,12])

arr.insert(0,1)

print(‘Array after inserting element at first position:’,arr)

print(‘index of last element: ’,arr.index(arr[-1]))

Output:

Array after inserting element at first position: array(‘i’,[1,1,3,6,12])

index of last element: 3

When you need to add multiple items or an array of items ( of same data type as of array) to your array, use the extend() method like this

# Example program to add multiple items using extend method


arr = array.array(‘i’,[1,6,23,67])

arr.extend([3,56,2])

print(‘array after extension: ’,arr)

Output:

Array after extension: array(‘i’,[1,6,23,67,3,56,2])

This can be done with concatenation as well, using + operator between the two 

arrays

# Example program for array concatenation


arr_1 = array.array(‘i’,[1,6,23,7])

arr_2 = array.array(‘i’,[1,36,529,49])

arr_3 = arr_1 + arr_2


print(‘New array after concatenation: ‘,arr_3)


Output

New array after concatenation: array(‘i’,[1,6,23,7,1,36,529,49])

Removing elements from the array

The last element from the array can be removed using the pop() method.

# Example program 1 for removing element from array


arr_1 = array.array(‘i’,[1,6,23,7])

x = arr_1.pop()

print(‘popped element from the array: ‘,x)

print(‘length of array is now: ’)

Output:

popped element from the array: 7

Length of array is now: 3

Now we will remove a specific element from the array using remove() method.

# Example program 2 for removing element from array


arr_1 = array.array(‘i’,[1,6,23])

arr_1.remove(6)

print(‘New array is: ‘,arr_1)

print(‘length of array is now: ’)

Output

New array is: array(‘i’,[1,23])

Length of array is now: 2

This was all about converting lists to string and strings to lists. To get in-depth knowledge of Python along with its various applications and real-time projects, you can enroll in Python Course in Chennai or Python Training Institute in Bangalore by FITA at an affordable price, which includes real-time projects with certification, support and career guidance assistance.

 






Quick Enquiry