In this post, you will learn the Feature, Uses, and Advantages of Array in Python. Before we get started, if you want to Tuple in the python program, please go through the following article: TUPLE in Python.

What is Python Array?

A Python Array is a common type of data structure wherein all elements must be of the same data type. In Python programming, an array can be handled by the “array” module. If you create arrays using the array module, elements of the array must be of the same numeric type.

When to use Array in Python?

Python arrays are used when you need to use many variables which are of the same type. It can also be used to store a collection of data. The arrays are especially useful when you have to process the data dynamically. Python arrays are much faster than list as it uses less memory.

 

Syntax to Create an Array in Python

You can declare an array in Python while initializing it using the following syntax.

The following image explains the syntax.

  1. Identifier: specify a name like usually, you do for variables
  2. Module: Python has a special module for creating arrays, called “array” – you must import it before using it
  3. Method: the array module has a method for initializing the array. It takes two arguments, type code, and elements.
  4. Type Code: specify the data type using the type codes available (see list below)
  5. Elements: specify the array elements within the square brackets, for example [130,450,103]

How to create an Array in Python?

In python we use the following syntax to create arrays:

For Example

The above code creates an array having an integer type. The letter’ is a type code. The following tables show the type codes:

Type code Python type C Type Min size(bytes)
‘u’ Unicode character Py_UNICODE 2
‘b’ Int Signed char 1
‘B’ Int Unsigned char 1
‘h’ Int Signed short 2
‘l’ Int Signed long 4
‘L’ Int Unsigned long 4
‘q’ Int Signed long long 8
‘Q’ Int Unsigned long long 8
‘H’ Int Unsigned short 2
‘f’ Float Float 4
‘d’ Float Double 8
‘i’ Int Signed int 2
‘I’ Int Unsigned int 2

How to access array elements?

You can access any array item by using its index. The syntax is

For example,

Output:

The following image illustrates the basic concept of accessing array items by their index. Here, we have accessed the second value of the array using its index, which is 1. The output of this will be 200, which is basically the second value of the balanced array. The array index starts with 0. You can also access the last element of an array using the -1 index.

Example:

Output:

You can also access elements by using the ‘:’ operator.

Example:

Output:

This operation is called a slicing operation.

How to insert elements?

Python array insert operation enables you to insert one or more items into an array at the beginning, end, or any given index of the array. This method expects two arguments index and value. The syntax is

Example:

Let’s add a new value right after the second item of the array. Currently, our balance array has three items 300, 200, and 100. Consider the second array item with a value of 200 and index 1. In order to insert the new value right “after” index 1, you need to reference index 2 in your insert method, like this:

Output:

Example 2:

Output:

How to modify elements?

In Python, arrays are mutable. They can be modified by the following syntax:

Example:

Output:

We can also perform concatenation operations on arrays.

Example:

Output:

The above code Concat two variables called “first” and “second”. The result is stored in a variable called “number”. The last line of code is used to print two arrays.

How to pop elements?

In Python, a developer can use an array.pop([a]]) syntax to pop element from array. Example:

Output:

You can also use the ‘del’ statement of Python.

Example

Output:

How to delete elements?

With this operation, you can delete one item from an array by value. This method accepts only one argument, value. After running this method, the array items are re-arranged, and indices are re-assigned. The syntax is

Example:

Let’s remove the value of “3” from the array

Output:

How to Search and get the index of a value in an Array

With this operation, you can search for an item in an array based on its value. This method accepts only one argument, value. It is a non-destructive method, which means it does not affect the array values. The syntax is

Example:

Let’s find the value of “3” in the array. This method returns the index of the searched value.

Output:

This operation will return the index of the first occurrence of the mentioned element.

How to Reverse an Array?

This operation will reverse the entire array. Syntax: array.reverse()

Output:

 

Convert array to Unicode

Python array can be converted to Unicode. To fulfill this need, the array must be a type ‘u’; otherwise, you will get “ValueError.”. Example:

Output:

Count the occurrence of a Value in Array

You can also count the occurrence of elements in the array using the array.count(x) syntax. Example:

Output:

Traverse an Array

You can traverse a python array by using loops, like this one:

Output:

Feature, Uses, and Advantages of Array in Python

The article was published on September 20, 2020 @ 9:51 AM

Leave a Comment