You are on page 1of 12

Array Data Structure

What is an Array?
• Arrays are defined as the collection of similar types of data items
stored at contiguous memory locations.
• It is one of the simplest data structures where each data element
can be randomly accessed by using its index number.
• For example, if we want to store the marks of a student in 6
subjects, then we don't need to define a different variable for the
marks in different subjects.
• Instead, we can define an array that can store the marks in each
subject at the contiguous memory locations.
Properties of arrays
i. Each element in an array is of the same data type and carries the
same size that is 4 bytes.
ii. Elements in the array are stored at contiguous memory locations
from which the first element is stored at the smallest memory
location.
iii. Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
base address and the size of the data element
Representation of an array
• We can represent an array in various ways in different programming
languages.
• Below are some operations that can be performed in an array:
i. append()
ii. insert()
iii. pop()
iv. remove()
v. index()
vi. reverse()
Python append() Method
• This function is used to add the value mentioned in its arguments at
the end of the array.
• In this below example, we are adding an element at the end of the
array.
• Example
Array insert(i,x) Method in Python
• This function is used to add the value(x) at the ith position specified in
its argument.
• In this given example below, we are inserting an element at a
specified index in an array.
• Example
Array pop() Method in Python
• This function removes the element at the position mentioned in its
argument and returns it.
• In this given example, we are removing an element mentioned at a
particular index.
Example
Array remove() in Python
• This function is used to remove the first occurrence of the value
mentioned in its arguments.
• In this given example, we are removing the first occurrence of 1
from a given array.
Example4
Array index() Method
• This function returns the index of the first occurrence of the value
mentioned in the arguments.
• In this given example, we are finding the index of 1st occurrence of 2
and printing it.
• Example 5
Array reverse() method
• This function reverses the array. In this given example, we are
reversing the array by using reverse().
• Example Array reverse
Application of Arrays
• Arrays should be used where the number of elements to be stored is
already known.
• Arrays are commonly used in computer programs to organize data so
that a related set of values can be easily sorted or searched.
• Generally, when we require very fast access times, we usually prefer
arrays since they provide O(1) access times.
• Arrays work well when we have to organize data in multidimensional
format. We can declare arrays of as many dimensions as we want.
• If the index of the element to be modified is known beforehand, it can
be efficiently modified using arrays due to quick access time and
mutability.
2D arrays in Python

You might also like