For Enquiry: 93450 45466

What are Python Arrays and how to use them?



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.

 





  • Trending Courses

    JAVA Training In Chennai Software Testing Training In Chennai Selenium Training In Chennai Python Training in Chennai Data Science Course In Chennai Digital Marketing Course In Chennai DevOps Training In Chennai German Classes In Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai UI UX Design course in Chennai Tally course in Chennai Full Stack Developer course in Chennai Salesforce Training in Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical Hacking course in Chennai RPA Training In Chennai Cyber Security Course in Chennai IELTS Coaching in Chennai Graphic Design Courses in Chennai Spoken English Classes in Chennai Data Analytics Course in Chennai

    Spring Training in Chennai Struts Training in Chennai Web Designing Course In Chennai Android Training In Chennai AngularJS Training in Chennai Dot Net Training In Chennai C / C++ Training In Chennai Django Training in Chennai PHP Training In Chennai iOS Training In Chennai SEO Training In Chennai Oracle Training In Chennai Cloud Computing Training In Chennai Big Data Hadoop Training In Chennai UNIX Training In Chennai Core Java Training in Chennai Placement Training In Chennai Javascript Training in Chennai Hibernate Training in Chennai HTML5 Training in Chennai Photoshop Classes in Chennai Mobile Testing Training in Chennai QTP Training in Chennai LoadRunner Training in Chennai Drupal Training in Chennai Manual Testing Training in Chennai WordPress Training in Chennai SAS Training in Chennai Clinical SAS Training in Chennai Blue Prism Training in Chennai Machine Learning course in Chennai Microsoft Azure Training in Chennai Selenium with Python Training in Chennai UiPath Training in Chennai Microsoft Dynamics CRM Training in Chennai VMware Training in Chennai R Training in Chennai Automation Anywhere Training in Chennai GST Training in Chennai Spanish Classes in Chennai Japanese Classes in Chennai TOEFL Coaching in Chennai French Classes in Chennai Informatica Training in Chennai Informatica MDM Training in Chennai Big Data Analytics courses in Chennai Hadoop Admin Training in Chennai Blockchain Training in Chennai Ionic Training in Chennai IoT Training in Chennai Xamarin Training In Chennai Node JS Training In Chennai Content Writing Course in Chennai Advanced Excel Training In Chennai Corporate Training in Chennai Embedded Training In Chennai Linux Training In Chennai Oracle DBA Training In Chennai PEGA Training In Chennai Primavera Training In Chennai Tableau Training In Chennai Spark Training In Chennai Appium Training In Chennai Soft Skills Training In Chennai JMeter Training In Chennai Power BI Training In Chennai Social Media Marketing Courses In Chennai Talend Training in Chennai HR Courses in Chennai Google Cloud Training in Chennai SQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai Business Analytics Course in Chennai NextJS Course in Chennai Vue JS Course in Chennai

  • Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!