• Chennai, Bangalore & Online: 93450 45466Coimbatore: 95978 88270Madurai: 97900 94102

  • 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.

     






    Quick Enquiry

    Contact Us

    Chennai

      93450 45466

    Bangalore

     93450 45466

    Coimbatore

     95978 88270

    For Hiring

     93840 47472
     hr@fita.in

    Corporate Training

     90036 23340


    FITA Academy Branches

    Chennai
    Bangalore
    Coimbatore
    Other Locations
    FITA Academy - Velachery
    Plot No 7, 2nd floor,
    Vadivelan Nagar,
    Velachery Main Road,
    Velachery, Chennai - 600042
    Tamil Nadu

        :   93450 45466

    FITA Academy - Anna Nagar
    No 14, Block No, 338, 2nd Ave,
    Anna Nagar,
    Chennai 600 040, Tamil Nadu
    Next to Santhosh Super Market

        :   93450 45466

    FITA Academy - T Nagar
    05, 5th Floor, Challa Mall,
    T Nagar,
    Chennai 600 017, Tamil Nadu
    Opposite to Pondy Bazaar Globus

        :   93450 45466

    FITA Academy - Tambaram
    Nehru Nagar, Kadaperi,
    GST Road, West Tambaram,
    Chennai 600 045, Tamil Nadu
    Opposite to Saravana Jewellers Near MEPZ

        :   93450 45466

    FITA Academy - Thoraipakkam
    5/350, Old Mahabalipuram Road,
    Okkiyam Thoraipakkam,
    Chennai 600 097, Tamil Nadu
    Next to Cognizant Thoraipakkam Office and Opposite to Nilgris Supermarket

        :   93450 45466

    FITA Academy Marathahalli
    No 7, J J Complex,
    ITPB Road, Aswath Nagar,
    Marathahalli Post,
    Bengaluru 560037

        :   93450 45466

    FITA Academy - Saravanampatty
    First Floor, Promenade Tower,
    171/2A, Sathy Road, Saravanampatty,
    Coimbatore - 641035
    Tamil Nadu

        :   95978 88270

    FITA Academy - Singanallur
    348/1, Kamaraj Road,
    Varadharajapuram, Singanallur,
    Coimbatore - 641015
    Tamil Nadu

        :   95978 88270

    FITA Academy - Madurai
    No.2A, Sivanandha salai,
    Arapalayam Cross Road,
    Ponnagaram Colony,
    Madurai - 625016, Tamil Nadu

        :   97900 94102

  • Trending Courses

    JAVA Training In Chennai Dot Net Training In Chennai Software Testing Training In Chennai Cloud Computing Training In Chennai AngularJS Training in Chennai Big Data Hadoop Training In Chennai Android Training In Chennai iOS Training In Chennai Web Designing Course In Chennai PHP Training In Chennai Digital Marketing Course In Chennai SEO Training In Chennai

    Oracle Training In Chennai Selenium Training In Chennai Data Science Course In Chennai RPA Training In Chennai DevOps Training In Chennai C / C++ Training In Chennai UNIX Training In Chennai Placement Training In Chennai German Classes In Chennai Python Training in Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai Core Java Training in Chennai Javascript Training in ChennaiHibernate Training in ChennaiHTML5 Training in ChennaiPhotoshop Classes in ChennaiMobile Testing Training in ChennaiQTP Training in ChennaiLoadRunner Training in ChennaiDrupal Training in ChennaiManual Testing Training in ChennaiSpring Training in ChennaiStruts Training in ChennaiWordPress Training in ChennaiSAS Training in ChennaiClinical SAS Training in ChennaiBlue Prism Training in ChennaiMachine Learning course in ChennaiMicrosoft Azure Training in ChennaiUiPath Training in ChennaiMicrosoft Dynamics CRM Training in ChennaiUI UX Design course in ChennaiSalesforce Training in ChennaiVMware Training in ChennaiR Training in ChennaiAutomation Anywhere Training in ChennaiTally course in ChennaiReactJS Training in ChennaiCCNA course in ChennaiEthical Hacking course in ChennaiGST Training in ChennaiIELTS Coaching in ChennaiSpoken English Classes in ChennaiSpanish Classes in ChennaiJapanese Classes in ChennaiTOEFL Coaching in ChennaiFrench Classes in ChennaiInformatica Training in ChennaiInformatica MDM Training in ChennaiBig Data Analytics courses in ChennaiHadoop Admin Training in ChennaiBlockchain Training in ChennaiIonic Training in ChennaiIoT Training in ChennaiXamarin Training In ChennaiNode JS Training In ChennaiContent Writing Course in ChennaiAdvanced Excel Training In ChennaiCorporate Training in ChennaiEmbedded Training In ChennaiLinux Training In ChennaiOracle DBA Training In ChennaiPEGA Training In ChennaiPrimavera Training In ChennaiTableau Training In ChennaiSpark Training In ChennaiGraphic Design Courses in ChennaiAppium Training In ChennaiSoft Skills Training In ChennaiJMeter Training In ChennaiPower BI Training In ChennaiSocial Media Marketing Courses In ChennaiTalend Training in ChennaiHR Courses in ChennaiGoogle Cloud Training in ChennaiSQL Training In ChennaiCCNP Training in Chennai

  • Are You Located in Any of these Areas

    Adyar, Adambakkam, Anna Salai, Ambattur, Ashok Nagar, Aminjikarai, Anna Nagar, Besant Nagar, Chromepet, Choolaimedu, Guindy, Egmore, K.K. Nagar, Kodambakkam, Koyambedu, Ekkattuthangal, Kilpauk, Meenambakkam, Medavakkam, Nandanam, Nungambakkam, Madipakkam, Teynampet, Nanganallur, Navalur, Mylapore, Pallavaram, Purasaiwakkam, OMR, Porur, Pallikaranai, Poonamallee, Perambur, Saidapet, Siruseri, St.Thomas Mount, Perungudi, T.Nagar, Sholinganallur, Triplicane, Thoraipakkam, Tambaram, Vadapalani, Valasaravakkam, Villivakkam, Thiruvanmiyur, West Mambalam, Velachery and Virugambakkam.

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram 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!