Python Array Methods: Effective Array Operations

thumb_up 1  ·  sell Python array methods, Array operations in Python, Array manipulation with Python methods

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.

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

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

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])

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.

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])


The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back