oursolutionarchitectoursolutionarchitect
  • Python Questions and Answers
  • Python - Programming Examples
  • Python - Quick Guide
  • Python - Useful Resources
  • Python - Discussion
    • Selected Reading
    • Q&A

    Python - Array Methods


    The following are the built-in methods of Python array module -

    Python array.reverse() Method

    Like the sequence types, the array class also supports the reverse() method which rearranges the elements in reverse order.

    Syntax

    array.reverse()
    

    Parameters

    This method has no parameters

    Example

    import array as arr
    a = arr.array('i', [1, 2, 3, 4, 5])
    a.reverse()
    print (a)
    

    It will produce the following output

    array('i', [5, 4, 3, 2, 1])
    

    The array class also defines the following useful methods.

    Python array.count() Method

    The count() method returns the number of times a given element occurs in the array.

    Syntax

    array.count(v)
    

    Parameters

    • v − The value whose occurrences are to be counted

    Return value

    The count() method returns an integer corresponding the number of times v appears in the array.

    Example

    import array as arr
    a = arr.array('i', [1, 2, 3, 2, 5, 6, 2, 9])
    c = a.count(2)
    print ("Count of 2:", c)
    

    It will produce the following output

    Count of 2: 3
    

    Python array.index() method

    The index() method in array class finds the position of first occurrence of a given element in the array.

    Syntax

    array.index(v)
    

    Parameters

    • v − the value for which the index is to be found

    Example

    a = arr.array('i', [1, 2, 3, 2, 5, 6, 2, 9])
    c = a.index(2)
    print ("index of 2:", c)
    

    It will produce the following output

    index of 2: 1
    

    Python array.fromlist() Method

    The fromlist() method appends items from a Python list to the array object.

    Syntax

    array.fromlist(l)
    

    Parameters

    • i − The list, items of which are appended to the array. All items in the list must be of same arrtype.

    Example

    import array as arr
    a = arr.array('i', [1, 2, 3, 4, 5])
    lst = [6, 7, 8, 9, 10]
    c = a.fromlist(lst)
    print (a)
    

    It will produce the following output

    array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    

    Python array.tofile() Method

    The tofile() method in array class writes all items (as machine values) in the array to the file object f.

    Syntax

    array.tofile(f)
    

    Parameters

    • f − the file object obtained with open() function. The file to be opened in wb mode.

    Example

    import array as arr
    f = open('list.txt','wb')
    arr.array("i", [10, 20, 30, 40, 50]).tofile(f)
    f.close()
    

    Output

    After running the above code, a file named as "list.txt" will be created in the current directory.

    Python array.fromfile() Method

    The fromfile() method reads a binary file and appends specified number of items to the array object.

    Syntax

    array.fromfile(f, n)
    

    Parameters

    • f − The file object referring to a disk file opened in rb mode

    • n − number of items to be appended

    Example

    import array as arr
    a = arr.array('i', [1, 2, 3, 4, 5])
    f = open("list.txt", "rb")
    a.fromfile(f, 5)
    print (a)
    

    It will produce the following output

    array('i', [1, 2, 3, 4, 5, 10, 20, 30, 40, 50])