Site icon DataFlair

Python Program on Array Methods

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In this article, we will explore a Python program that demonstrates various methods of the array module. The array module provides an efficient way to store and manipulate arrays in Python. The program covers essential methods like pop(), tolist(), append(), index(), remove(), count(), reverse(), and insert(). Understanding these methods is crucial for effective array manipulation in Python.

Prerequisites

Topic Explanation

This program commences by initializing an integer array using the array module, setting the stage for exploring its functionalities. It demonstrates various methods within the module to alter and handle the array efficiently. These include pop(), which eliminates the array’s last element, and tolist(), a method for transforming the entire array into a list format. Other methods like append() are used for incorporating new elements into the array.

Further, the program explores additional capabilities of the array module. It employs the index() function to determine the position of a specific element, and remove() to eliminate an element’s first occurrence. For tallying how many times an element appears, count() is utilized. Additionally, reverse() facilitates the inversion of the array’s order, while insert() allows for the addition of elements at designated positions, enhancing the array’s versatility.

Code:

# Methods of Array module
import array as arr

# Creating an array of integers
myarr = arr.array('i', [10, 20, 30, 6, 77, 55, 88, 66, 99, 44, 1001])

# Printing the array
print(myarr)

# The below line of code removes the last element from myarr using pop()
myarr.pop()

# Converting the array to a list
mylist = myarr.tolist()

# Appending "Data Flair" to the list
mylist.append("Data Flair")

# Printing the modified list
print(mylist)

# Finding the index of the first occurrence of 77 in the array
print(mylist.index(77))  # Use mylist instead of myarr

# Displaying the length of the array before removing an element
print("Before remove ", len(mylist))  # Use mylist instead of myarr

# Removing the first occurrence of 77 from the list
mylist.remove(77)  # Use mylist instead of myarr

# Displaying the length of the array after removing an element
print("After remove ", len(mylist))  # Use mylist instead of myarr

# Printing the modified array
print(mylist)

# Taking user input for the limit of the array
n = int(input("Input size of array"))

# Taking user input for array elements
print("Enter elements in the array")
for i in range(n):
    x = int(input())
    mylist.append(x)  # Use mylist instead of myarr

# Taking user input for a number to search in the array
s = int(input("Enter a number for search"))

# Counting the occurrences of the specified number in the list
n = mylist.count(s)  # Use mylist instead of myarr

# Checking if the number was found or not
if n > 0:
    print("Searching success")
else:
    print("Searching not success")

# Reversing the list
mylist.reverse()  # Use mylist instead of myarr

# Inserting the number 720 at the beginning of the list
mylist.insert(0, 720)  # Use mylist instead of myarr

# Counting the occurrences of the number 720 in the list
m = mylist.count(720)  # Use mylist instead of myarr

# Printing the modified list
print(mylist)

Output:

array(‘i’, [10, 20, 30, 6, 77, 55, 88, 66, 99, 44, 1001])
[10, 20, 30, 6, 77, 55, 88, 66, 99, 44, ‘Data Flair’]
4
Before remove 11
After remove 10
[10, 20, 30, 6, 55, 88, 66, 99, 44, ‘Data Flair’]
Input size of array4
Enter elements in the array
2
3
4
5
Enter a number for search5
Searching success
[720, 5, 4, 3, 2, ‘Data Flair’, 44, 99, 66, 88, 55, 6, 30, 20, 10]

Code Explanation:

  • Creates an array of integers called myarr with predefined values
  • Prints out myarr to show the array contents
  • Uses myarr.pop() to remove the last element from myarr
  • Converts myarr to a list called mylist
  • Appends “Data Flair” string to mylist
  • Prints the modified mylist
  • Gets index of first occurrence of 77 in mylist
  • Prints mylist length before and after removing 77
  • Removes first occurrence of 77 from mylist
  • Takes user input for array size and elements to append
  • Searches mylist for user input number
  • Prints success or not based on search result
  • Reverses order of mylist
  • Inserts 720 at beginning of mylist
  • Counts occurrences of 720 in mylist
  • Prints final modified mylist

Summary

In summary, this Python program offers a practical example of using methods from the array module for array manipulation. It demonstrates how these methods can be applied to create, modify, and search arrays efficiently. Understanding the array module and its methods enhances one’s ability to work with arrays in Python, making data manipulation more convenient and efficient.

Exit mobile version