Get The Most Affordable Hosting in the World!
Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.
Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!
Get Started Now!array.reverse() Method
Like the sequence types, the array class also supports the reverse() method which rearranges the elements in reverse order.
Syntax
Parameters
This method has no parameters
Example
It will produce the following output −
array('i', [5, 4, 3, 2, 1])
The array class also defines the following useful methods.
array.count() Method
The count() method returns the number of times a given element occurs in the array.
Syntax
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
It will produce the following output −
Count of 2: 3
array.index() method
The index() method in array class finds the position of first occurrence of a given element in the array.
Syntax
Parameters
-
v − the value for which the index is to be found
Example
It will produce the following output −
index of 2: 1
array.fromlist() Method
The fromlist() method appends items from a Python list to the array object.
Syntax
Parameters
-
i − The list, items of which are appended to the array. All items in the list must be of same arrtype.
Example
It will produce the following output −
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
array.tofile() Method
The tofile() method in array class writes all items (as machine values) in the array to the file object f.
Syntax
Parameters
-
f − the file object obtained with open() function. The file to be opened in wb mode.
Example
Output
After running the above code, a file named as "list.txt" will be created in the current directory.
array.fromfile() Method
The fromfile() method reads a binary file and appends specified number of items to the array object.
Syntax
Parameters
-
f − The file object referring to a disk file opened in rb mode
-
n − number of items to be appended
Example
It will produce the following output −
array('i', [1, 2, 3, 4, 5, 10, 20, 30, 40, 50])
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.