In Python, arrays are implemented using the array module, which provides a range of methods for working with arrays. Some of the most commonly used methods are:
append(): adds an element to the end of the array.
extend(): adds the elements of an iterable (e.g., another array, list, or tuple) to the end of the array.
insert(): inserts an element at a specific position in the array.
remove(): removes the first occurrence of an element from the array.
pop(): removes and returns the element at a specific position in the array.
index(): returns the index of the first occurrence of an element in the array.
count(): returns the number of occurrences of an element in the array.
reverse(): reverses the order of the elements in the array.
sort(): sorts the elements in the array in ascending order (or in descending order if the
reverse argument is set to True).
import array
# Create an array of integers
my_array = array.array('i', [1, 2, 3, 4, 5])
# Append an element to the array
my_array.append(6)
# Insert an element at position 1
my_array.insert(1, 0)
# Remove the first occurrence of the element 3
my_array.remove(3)
# Print the index of the first occurrence of the element 4
print(my_array.index(4))
# Reverse the order of the elements in the array
my_array.reverse()
# Sort the elements in the array in descending order
my_array.sort(reverse=True)
# Print the final array
print(my_array)
This example creates an array of integers using the array() function from the
array module. We then use several of the array methods to modify the array, including
append(), insert(), remove(),
index(), reverse(), and sort(). Finally, we print the contents of the modified array.
Aryan Kumar
01-May-2023In Python, arrays are implemented using the array module, which provides a range of methods for working with arrays. Some of the most commonly used methods are:
This example creates an array of integers using the array() function from the array module. We then use several of the array methods to modify the array, including append(), insert(), remove(), index(), reverse(), and sort(). Finally, we print the contents of the modified array.
Prakash nidhi Verma
04-Jul-2018Method function in python :
append()
clear()
copy()
count()
extend()
index()
insert()
pop()
remove()
reverse()
sort()