Array Program
Array Program
Creating a Array
Elements can be removed from the array by using built-in remove() function but an Error
arises if element doesn’t exist in the set. Remove() method only removes one element at a
time, to remove range of elements, iterator is used. pop() function can also be used to
remove and return an element from the array, but by default it removes only the last
element of the array, to remove element from a specific position of the array, index of the
element is passed as an argument to the pop() method.
Note – Remove method in List will only remove the first occurrence of the searched
element.
# Python program to demonstrate
import array
print ("\r")
print (arr.pop(2))
print("\r")
Output:
In Python array, there are multiple ways to print the whole array with all the elements, but
to print a specific range of elements from the array, we use Slice operation. Slice
operation is performed on array with the use of colon(:). To print elements from beginning
to a range use [:Index], to print elements from end use [:-Index], to print elements from
specific Index till the end use [Index:], to print elements within a range, use [Start
Index:End Index] and to print whole List with the use of slicing operation, use [:]. Further,
to print whole array in reverse order, use [::-1].
# creating a list
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = arr.array('i', l)
for i in (a):
Sliced_array = a[3:8]
print(Sliced_array)
# Print elements from a
Sliced_array = a[5:]
print(Sliced_array)
Sliced_array = a[:]
print(Sliced_array)
Output :
0 Intial Array:
1 2 3 4 5 6 7 8 9 10
import array
print ("\r")
print (arr.index(2))
print (arr.index(1))
Output: