Signup/Sign In
LAST UPDATED: DECEMBER 7, 2019

Arrays in Python - Part 2

    In part 1 of this series, we understood what an array in Python is, how it is different from lists, how they can be created and how they can be accessed using indexing. If you haven't seen that post, it is strongly suggested to go through it and understand the concepts and code in its entirety.

    In this post, we will look at how arrays can be sliced, similar to list slicing in python. We will then look at how an element can be added or changed in a list, followed by the deletion of elements of an array.




    1. Array slicing in Python

    Array slicing in Python is similar to list slicing. Let's take a code example and see,


    Time for an example:

    import array as ar
    
    my_array = [1.1, 4.5, 6.8, 9.4, 5.8, 3.0]
    display_array = ar.array('d', my_array)
    print(display_array)
    # array slicing
    print(display_array[1:4])
    # 2nd element from behind
    print(display_array[-2])
    # elements from 2nd index till end
    print(display_array[2:])
    # all the elements
    print(display_array[:])
    # 2 elements from the beginning
    print(display_array[:2])

    Output:

    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    array('d', [4.5, 6.8, 9.4])
    5.8
    array('d', [6.8, 9.4, 5.8, 3.0])
    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    array('d', [1.1, 4.5])



    2. Adding and changing elements in Python

    Now let's see a few different ways of adding a new data element to an array and to modify an existing array.


    2.1 Changing elements of an array using its index

    You can simply use the equal to operator and change any array element by accessing it using its index within square brackets,

    import array as ar
    
    my_array = [1.1, 4.5, 6.8, 9.4, 5.8, 3.0]
    display_array = ar.array('d', my_array)
    print(display_array)
    
    display_array[0] = 0.0 
    
    print("\n After changing")
    print(display_array)
    
    display_array[1:4] = ar.array('d', [2.2, 3.3, 4.4])
    
    print("\n Changing again")
    print(display_array)

    Output:

    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    
    After changing
    array('d', [0.0, 4.5, 6.8, 9.4, 5.8, 3.0])
    
    Changing again
    array('d', [0.0, 2.2, 3.3, 4.4, 5.8, 3.0])

    2.2 Using the append and extend methods

    We can use the append and the extend methods to add data to an existing array in python.

    import array as ar
    
    my_array = [1.1, 4.5, 6.8, 9.4, 5.8, 3.0]
    display_array = ar.array('d', my_array)
    print(display_array)
    
    print("\n Using append method")
    display_array.append(-1.1)
    print(display_array)
    
    print("\n Using extend method")
    display_array.extend([-8.96])
    print(display_array)
    

    Output:

    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    
    Using append method
    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0, -1.1])
    
    Using extend method
    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0, -1.1, -8.96])

    2.3 Using the + concatenation operator

    We can use the concatenation operator + to add two arrays and form one array or add a new data element to an array.

    import array as ar
    
    my_array_one = [1.1, 4.5, 6.8] 
    my_array_two = [9.4, 5.8, 3.0]
    display_array_one = ar.array('d', my_array_one)
    display_array_two = ar.array('d', my_array_two)
    
    print(display_array_one)
    print(display_array_two)
    
    my_array = display_array_one + display_array_two
    
    print("\n After concatenation")
    print(my_array)
    

    Output:

    array('d', [1.1, 4.5, 6.8])
    array('d', [9.4, 5.8, 3.0])
    
     After concatenation
    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])



    3. Deletion of elements in an array in Python

    Now let's see how we can delete or remove an element from a python array.


    3.1 Using the del keyword to delete a specific element

    import array as ar
    
    my_array = [1.1, 4.5, 6.8, 9.4, 5.8, 3.0]
    display_array = ar.array('d', my_array)
    print(display_array)
    
    # deleting the 4th element of array
    del display_array[3]
    
    print(display_array)

    Output:

    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    array('d', [1.1, 4.5, 6.8, 5.8, 3.0])

    3.2 Using the del keyword to delete an array

    The del keyword can also be used to delete the complete python array.

    import array as ar
    
    my_array = [1.1, 4.5, 6.8, 9.4, 5.8, 3.0]
    display_array = ar.array('d', my_array)
    print(display_array)
    
    del display_array
    
    print(display_array)

    Output:

    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    Traceback (most recent call last):
    
      File "<ipython-input-15-7d6290fac1bc>", line 6, in <module>
        print(display_array)
    
    NameError: name 'display_array' is not defined

    Note: After deleting the array, trying to display it gives an error.


    3.3 Using remove method

    We can use the remove method too to remove an element from a python array. We can remove a data element from the array by providing the value of the data element to be removed as the argument to the remove() method.

    import array as ar
    
    my_array = [1.1, 4.5, 6.8, 9.4, 5.8, 3.0]
    display_array = ar.array('d', my_array)
    print(display_array)
    
    display_array.remove(6.8)
    print(display_array)
    

    Output:

    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    array('d', [1.1, 4.5, 9.4, 5.8, 3.0])

    3.4 Using the pop method

    We can use the pop() method to remove the data element from an array in python. The pop() method takes the index of the element to be removed as an argument. If you provide negative value as an argument then the index is counted from the rear end of the array.

    import array as ar
    
    my_array = [1.1, 4.5, 6.8, 9.4, 5.8, 3.0]
    display_array = ar.array('d', my_array)
    print(display_array)
    
    display_array.pop(-1)
    print(display_array)

    Output:

    array('d', [1.1, 4.5, 6.8, 9.4, 5.8, 3.0])
    array('d', [1.1, 4.5, 6.8, 9.4, 5.8])

    If you provide an index value which is not a valid index value for the given array, then you will get IndexError.

    IndexError: pop index out of range



    Conclusion

    In this post, we understood how arrays can be sliced, how they can be updated and how they can be deleted. Don't forget to run the code in your IDE and experiment with it.

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS