Python Array
Python Array
• print()
• a.insert(1, 4)
• print("Array after insertion : ", end=" ")
• for i in (a):
• print(i, end=" ")
• print()
• Accessing elements from the Array
• In order to access the array items refer to the
index number. Use the index operator [ ] to access
an item in a array. The index must be an integer.
• import array as arr
• a = arr.array('i', [1, 2, 3, 4, 5, 6])
• print("Access element is: ", a[0])
• print("Access element is: ", a[3])
• b = arr.array('d', [2.5, 3.2, 3.3])
• print("Access element is: ", b[1])
• print("Access element is: ", b[2])
Removing Elements from the 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.
Removing Element using remove() function:
import array as arr
a = arr.array('i', [1, 2, 3, 4, 5,1, 6])
a.remove(1)
for i in (a):
print(i,end=" ") 234516
print()