Array Manipulations


Array Manipulations: Adding, Deleting, and
Accessing Elements


What is an Array?

An array is a data structure that stores a fixed-size sequential collection of elements of the same type. It's used to store multiple values in a single variable, making it easier to manage large sets of data.

Characteristics of Arrays


  • Once an array is created, its size cannot be changed.  The number of elements an array can hold is set at the time of its creation. This means you cannot add more elements than the initial size allocated to the array. If you need a larger array, you have to create a new one and copy the existing elements into it.

  • Elements in an array are accessed using indices, starting from 0. The first element of an array is at index 0, the second at index 1, and so on. This allows for direct access to any element in the array by specifying its index.

  • All elements in an array are of the same type. Arrays store elements of a single data type, such as integers, floats, or strings. This ensures that each element in the array is treated consistently, allowing for efficient memory allocation and processing.

·

Types of Arrays


One-Dimensional Array:

A one-dimensional array is a linear sequence of elements, meaning it has a single row of elements. It's essentially a list where each element is stored at a specific position and can be accessed using an index.

                 



Multi-Dimensional Array:


A multi-dimensional array is an array of arrays. The most common type is a two-dimensional array, which can represent a matrix. In a multi-dimensional array, each element itself is an array. This structure allows for the representation of data in a grid-like format, such as rows and columns in a table.

                         





  • The array named 'matrixhas a size of 5 rows and 5 columns.
  • It can be written as a 5x5 array. 
  • The size of the array is 5x5. 
  • The element in 'matrix' at position (0,0) is 50.



Array Manipulations in Python


One-Dimensional Arrays in Python


  • Creating One Dimensional Arrays

In Python, arrays can be created using ' lists ' or the 'arraymodule for more array-specific operations. However, lists are more commonly used due to their flexibility. 




    1. Creating One Dimensional Arrays using ' lists '



arr = [10, 20, 30, 40, 50]

print(arr)

     

   2. 
Creating  One Dimensional Arrays using  ' array'  module




import array
arr = array.array('i', [10, 20, 30, 40, 50])  
print(arr)





  • Inserting elements 

    1. Inserting elements at the end 




import array

arr = array.array('i', [10, 20, 30, 40, 50])  
arr.append(12)
print(arr)

          
         Output :   array('i', [10, 20, 30, 40, 50, 12 ] )


    2. Inserting an element at a specific position




import array

arr = array.array('i', [10, 20, 30, 40, 50])  
arr.append(12)

# Inserting an element at a specific position

arr.insert(2, 25)
print(arr)

            
       
       Output :  array('i', [10, 20, 25, 30, 40, 50, 12])



  • Accessing elements



arr = [10, 20, 30, 40, 50]
print(arr[0])  

       
        Output :  10


  • Traversing the array 

   

arr = [10, 20, 30, 40, 50]
for element in arr:
    print(element)

     Output : 10

                   20

                   30

                   40

                   50



  • Deleting elements 

In Python, can use lists, which are dynamic and allow easy deletion of elements. You can delete elements using the ' del ' statement, the ' remove ' method, or the 'pop' method.

    1. Deleting an element by index using 'del'



arr = array.array('i', [10, 20, 30, 40, 50])  

# Deleting an element by index using 'del'
del arr[2]  
print(arr)

        Output :   array('i', [10, 20, 40, 50])


     2. Deleting an element by value using 'remove'



arr = array.array('i', [10, 20, 30, 40, 50])  


# Deleting an element by value using 'remove'

arr.remove(40)
print(arr)

       
        Output :  array('i', [10, 20, 30, 50])



     3. Deleting an element by index using 'pop'



arr = array.array('i', [10, 20, 30, 40, 50])

#Deleting an element by index using 'pop'
arr.pop(1)  
print(arr)

       
          Output : array('i', [10, 30, 40, 50])



Two-Dimensional Arrays in Python


  • Creating Two Dimensional Arrays using lists



matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print(matrix[0][0])


     Output : 1



  • Inserting elements 


In Python, two-dimensional arrays can be represented using lists of lists. Inserting an element can be done using list operations.


       1. Function to insert a new row at a specific index 



matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Function to insert a new row at a specific index
def insert_row(matrix, index, new_row):
    matrix.insert(index, new_row)

new_row = [10, 11, 12]
insert_row(matrix, 1, new_row)


# Traversing the matrix
for row in matrix:
  for element in row:
       print(element)

      Output : 1    2    3   10    11    12    4    5    6    7    8     9




     2. Function to insert a new element in a specific row and column




matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

def insert_element(matrix, row, col, new_element):
    matrix[row].insert(col, new_element)

# Inserting a new element
insert_element(matrix, 2, 1, 99)

# Traversing the matrix
for row in matrix:
  for element in row:
       print(element)

        Output :  1     2     3     4    5      6      7      99      8      9


  • Accessing elements


print(matrix[0][0])

print(matrix[1][2])

      
      Output :    1

                         6


  • Traversing the array    



matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
# Traversing the matrix
for row in matrix:
  for element in row:
       print(element)


    Output :  1     2    3    4     5     6     7      8     9


  • Deleting elements 

   1.   Delete a row at a specific index


matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]


# Function to delete a row at a specific index
def delete_row(matrix, index):
    del matrix[index]



# Deleting the second row (index 1)
delete_row(matrix, 1)



# Printing the modified matrix
print("Matrix after deleting row:")
for row in matrix:
    print(row)

     
     Output :   
                       Matrix after deleting row:
                       [1, 2, 3]
                       [7, 8, 9]


     2. Deleting a Specific Element in a Row



matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]


# Function to delete an element in a specific row and column
def delete_element(matrix, row, col):
    del matrix[row][col]



# Deleting the element at row 1, column 1 (element 5)
delete_element(matrix, 1, 1)



# Printing the modified matrix
print("Matrix after deleting row:")
for row in matrix:
    print(row)

              Output :  

                              Matrix after deleting element:

                             [1, 2, 3]

                             [4, 6]

                             [7, 8, 9]



    3. Combining Row and Element Deletion



matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

def delete_row(matrix, index):
    del matrix[index]

# Function to delete an element in a specific row and column
def delete_element(matrix, row, col):
    del matrix[row][col]

# Deleting the second row (index 1)
delete_row(matrix, 1)

# Deleting the element at row 1, column 1 (element 8 in the modified matrix)
delete_element(matrix, 1, 1)


print("Matrix after deleting row:")
for row in matrix:
    print(row)

        Output  :  

                         Matrix after deleting row and element:

                         [1, 2, 3]

                         [7, 9]




Comments

Popular posts from this blog

Object-Oriented Programming

The Strategic Importance of Agile Methodology in Contemporary Software Development