What are Python Arrays?

An array is simply a data structure that has the capability to contain multiple values at a time. It is a series of elements in a specific order, the collection of elements of the same type. This container holds a specific number of elements of the same datatype at contiguous memory locations.

How to create an array in python?

To create an array, we are supposed to import an array module. Array like object of float, integer, and Unicode characters can be created using the python array module. Let’s look at the basic syntax for the same. “d” is type code for float type for both float and double, “i” is type code for integer type for char, short, int, long, and “u” is type code for Unicode type.

Syntax:  array_name= array (data_type, value_list),     #data_type and value_list are the arguments we need to specify.

Based on the syntax, arrays are created below:

  • input

#Python program demonstrating array creation

import array as arr       #arr is a short name for array here, it can be anything
a= arr(‘i’, [4,5,6])     #array of integer type is created
print (“Array of integer type: “, a)   #original array is printed
for i in range (0,3):
print(a[i], end=”, “)
print()

output

Array of integer type: 4, 5, 6

  • input

import array as arr       #arr us a short name for array here, it can be anything
a= arr(‘d’, [4.5,3.4,2.6])     #array of float type is created
print (“Array of float type: ” , a)   #original array is printed
for i in range (0,3):
print(a[i], end=”,”)
print()

output

Array of float type: 4.5, 3.4, 2.6

How can we find the length of a python array?

This method is used to find the length i.e. number of elements in an array.

Syntax:

len(array_name)

Example:

Input:

import array
arr=array.array(‘i’,[1,3,4])
print(len(arr))

output

3

How can we determine item size in the python array?

We can also determine the number of bytes that are taken by one item, internally, in a python array.

Syntax:

array_name.itemsize

Example:

input

import array
arr=array.array(‘i’,[1,3,4])
print(arr.itemsize)

output

4

How can we determine type code in the python array?

We can also determine type code character in a python array.

Syntax:

Array.typecodes

Example:

input

import array
arr=array.array(‘i’,[1,3,4])
print((array.typecodes))

output

bBuhHiIlLqQfd

How can we determine the location of a python array?

We can also determine the location as well length in a python array.

Syntax:

Array_name.buffer_info()

Example:

input
import array
arr=array.array(‘i’,[1,3,4])
print((arr.buffer_info()))

output

(2099431740288, 3) 

How can we access python array Elements?

1). We are supposed to use indices for accessing elements. The count begins from 0 and ends at one less than the length of an array.

Syntax:

array_name[indexNumber]

Example:

input

import array as arr
a=arr.array(‘i’, [2, 6, 9, 10])
print(“Element at first position: “, a[0])
print(“Element at third position: “, a[2])

output

Element at first position: 2
Element at third position: 9

2).  You can also start accessing elements from the end, for that the first element from the last i.e. last, is accessible by index -1.

Example:

input

import array as arr
a=arr.array(‘i’, [2, 6, 9, 10])
print(“Element at last position: “, a[-1])

output

Element at last position: 10

3). Another method is by specifying the range of index separated by ”:”. The execution stops at element one less than the number specified after the colon.

Example:

input

import array as arr
a=arr.array(‘i’, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(a[1:4])

output

Element at last position: 2, 3, 4

How can we add elements in a python array?

1). We can insert elements at the starting, end, or specified index in an array using insert operation. It expects two arguments i.e. index and value.

Syntax:

array_name.insert[indexNumber, value]

Example:

input

import array as arr
a=arr.array(‘i’, [1, 2, 3, 4, 5])
a.insert(2, 10)
print(a)

output

array(‘i’,[ 1, 2, 10, 3, 4, 5])

2). We can use append operation which adds the value at the last of an array.

Syntax:

array_name.append[value]

Example:

input

import array as arr
a=arr.array(‘i’, [1, 2, 3, 4, 5])
a.append(10)
print(a)

output

array(‘i’,[ 1, 2, 3, 4, 5, 10])

How can we modify elements in a python array?

Firstly, we need to know that arrays are mutable, i.e. modifiable.

Syntax:

object_name [indexNumber]=value

Example:

input

import array as arr
a=arr.array(‘i’, [1, 2, 3, 4, 5])
a[3]=33
print(a)

output

array(‘i’,[ 1, 2, 3, 33, 5])

How can we extend an array with new elements in python?

This operation attaches the specified array of elements at the end of the original one in order. The input has to be in an array format, separated by commas.

Syntax:

Array_name.extend([values])

Example:

input

import array as arr
a=arr.array(‘i’, [1, 2, 3, 4, 5])
a.extend([7,9,8])
print(a)

output

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

How can we join/ concatenate arrays in python?

We can concatenate two ore more arrays by using addition sign in between their variable names.

Syntax:

Array1_name+array2_name

Example:

input

import array as arr
a=arr.array(‘i’, [1, 2, 3, 4, 5])
b=arr.array(‘i’, 6, 7, 8, 9, 10])
new_array=arr.array(‘i’)
new_array=a+b
print(new_array)

output

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

How can we pop an element from python arrays?

We can pop an element using pop operation which takes either no or index’s number as a parameter. If no index’s number is specified, it removes the last element. It can also be used to return the deleted element/ value from the specified index.

Syntax:

array_name.pop[indexNumber]

Example:

input

import array as arr
a=arr.array(‘i’, [1, 2, 3, 4, 5])
a.pop(3)
print(a)

output

array(‘i’,[ 1, 2, 3, 5])

How can we delete an element from python arrays?

Deletion includes removal of the specified existing value and returning a new re-organized array. If the value does not exist, it returns an error. This operation does not return anything.

Note: Remove operation the specified value only from the first occurrence.

Syntax:

array_name.remove [ value]

Example:

input

import array as arr
a=arr.array(‘i’, [11, 22, 33, 44, 55])
a.remove(33)
print(a)

output

array(‘i’,[11, 22, 33, 44, 55])

How can we search and get the index of an element in python arrays?

Here we provide the value whose index we want to search in an array length. If no such value exists, the error is returned.

Syntax:

array_name.index [ value]

Example:

input

import array as arr
a=arr.array (‘i’, [11, 22, 33, 44, 55])
print(a.index(44))

output

3

How can we reverse python arrays?

Syntax:

Array.reverse()

Example:

input

import array as arr
a=arr.array(‘i’, [11, 22, 33, 44, 55])
a.reverse()
print(a)

output

array(‘i’,[55, 44, 33, 22, 11])

How can we count the occurrence of a value in python arrays?

We can check the count of the occurrence of a specified element/ value by specifying the value in count operation.

Syntax:

Array.count(value))

Example:

input

import array as arr
a=arr.array(‘i’, [11, 22, 33, 44, 22, 55, 22, 22])
print(a.count(22))

output

4

How can we traverse python arrays?

We can iterate in an array by below specified method.

Syntax:

For

Example:

input

import array as arr
a=arr.array(‘i’, [11, 22, 33, 44, 55])
for x in a:
print(x)

output

11
22
33
44
55

What are the Advantages of a python array?

  • Elements are easily accessible using index numbers.
  • The memory is saved as it can be allocated dynamically in an array. If memory is required, it gets allocated at the runtime of the code.
  • As the data type of elements is identical, the values are easily accessible from CPU cache. So, from this, we can conclude that iteration over the arrays is much faster as compared to other processes’ iterations. The array is parsed here.
  • Also due to the same data types, the system has complete précised and accurate awareness/ knowledge of the array’s address, and wherein stored memory is allocated. So this makes accessing fast as well as predictable.
  • The direct transverse option using indices in arrays, decreases the time consumption, makes debugging easier and also more optimal for usage.
  • Arrays are more compact to memory usage as compared to the hash table and linked list as array consumes memory only for the values, beginning address and for the length whereas linked list requires a pointer for each value which has been inserted, hash tables also consume memory based on their implementation, which usually requires extra space for allocation.
  • Arrays are a better option over variables as they are known for homogenous data collection in the same variable.
  • As arrays store data in a sequential manner which leads to an increase in efficiency, it is advantageous when non-sequential data structures are taken into consideration.
  • The assignment of a collection of values to a single variable makes the reusability of the code very easy. It also leads to an increase in the readability of the code.

So this is all about Python Arrays.

I hope you found this article useful.


You Might Also Like