Python’s standard data types list, tuple and string are sequences. A sequence object is an ordered collection of items. Each item is characterized by incrementing index starting with zero. Moreover, items in a sequence need not be of same type. In other words, a list or tuple may consist of items of different data type.
An array is also an indexed collection of items, but the items must be of similar data type.
Syntax:
import array
obj = array.array(typecode[, initializer])
Ex:
a = arr.array('i', [1, 2, 3])
append()
method adds a new element at the end of given array.insert()
method insert a new element at the specified index.extend()
method in array class appends all the elements from another array of same typecode.remove()
method removes the first occurrence of a given value from the arraypop()
method removes an element at the specified index from the array, and returns the removed element.reverse()
method which rearranges the elements in reverse order.count()
method returns the number of times a given element occurs in the array.index()
method in array class finds the position of first occurrence of a given element in the array.fromlist()
method appends items from a Python list to the array object.tofile()
method in array class writes all items (as machine values) in the array to the file object f.fromfile()
method reads a binary file and appends specified number of items to the array object.Joining Array
extend()
method to merge the two listsMain Page | Next Chapter: Python - Oops |