Working with Array in Python
Array:
Array is a collection of identical data items which are
stored in contiguous memory locations. The storage container of array
holds same type of elements together.
In Python array is handled with the module name array.
Terms used in Array manipulation:
Element:
Element is an individual data item stored in an array.
Index:
The index is a location of an array element on memory. The
index values are represented in numeric. The array index i.e. location of the
elements in an array starts from 0. The array elements are
accessed through the index values.
Creating Array in Python:
Array is created by importing array module in Python.
Syntax :
array_name=array.array(type code,[array elements])
here,
array_name: user defined name of the array
array: it is module in python which is imported and then used to
create array
type code: It is type code to be mentioned as per type of your
data items( for example, for int-'i', for double='d', for float-'f')
array elements: list of array elements
for example:
import array as ar
sample=ar.array('i',[1,2,3,4,5,6])
print(sample)
O/P:
array('i', [1, 2, 3, 4, 5, 6])
In this sample array integer type of array elements are stored.
Access Array Elements:
The array elements are accessed by using its index numbers.
Syntax:
array_name[index_num]
for example,
Access array element which are at second place of sample array.
import array as ar
sample=ar.array('i',[1,2,3,4,5,6])
print(sample[1])
O/P:
2
We can also access array element by using negative index. The last
element denotes -1 index, second last element denotes -2 index and so on.
for example,
sample=ar.array('i',[1,2,3,4,5,6])
print(sample[-2])
O/P:
5
We can access array elements by using : colon operator as follows,
sample=ar.array('i',[1,2,3,4,5,6])
print(sample[0:6])
O/P:
array('i', [1, 2, 3, 4, 5, 6])
sample=ar.array('i',[1,2,3,4,5,6])
print(sample[3:5])
O/P:
array('i', [4, 5])
Insert Array Elements:
Python array help in inserting array elements at the beginning, end or at any index of an array. Insert method is used to insert array element in an array. Just give an array index where you want to insert the new array element.
Syntax:
array_name.insert(index, value)
for example:
sample=ar.array('i',[1,2,3,4,5,6])
sample.insert(2,100)
print(sample)
O/P:
array('i', [1, 2, 100, 3, 4, 5, 6])
Initially at the second index there is value 3 but after inserting 100 at 2nd
index values are changed now.
Modify Array Elements:
object_name[index]=value
sample[0]=1000
print(sample)
O/P:
array('i', [1000, 2, 100, 3, 4, 5, 6])
Concatenation of Arrays:
We can combine two or more than two arrays
in Python as follows,
a=arr.array('i',[1,2,3,4])
d=a+b+c
Delete Array Elements:
An array items can be removed from an array and then array indexes are re assigned index and then rearranged on memory. This method only take one argument
Syntax:
array_name.remove(value)
For Example,
a=arr.array('i',[1,2,3,4,4])
a.remove(4)
print(a)
O/P:
array('i', [1, 2, 3, 4])
Remove Using pop():
a=arr.array('i',[1,2,3,4,4])
a.pop(2)
print(a)
O/P:
array('i', [1, 2, 4, 4])
The array element on 2nd index has been removed from array list.
Get Index of an Array Element:
We can search index of an array element in
Python as follows,
This method accepts only one argument.
Syntax:
array_name.index(value)
For example,
a=arr.array('i',[1,2,3,4,4])
a.index(4)
print(a)
O/P:
3
Reverse an Array :
Syntax:
array.reverse()
This method help to reverse entire array in
Python.
For Example,
a=arr.array('i',[1,2,3,4,4])
a.reverse()
print(a)
O/P:
array('i', [4, 4, 3, 2, 1])
Count The Occurrences of Array Elements:
The count function helps to count the number of times array elements occurred in an array.
a=arr.array('i',[1,2,3,4,4])
print(a.count(4))
O/P:
2
Sort Array Elements:
Sort() is used to sort array elements
for example,
more=[2,8,9]
more.sort()
print(more)
O/P:
[2, 8, 9]
Append Element to array list:
m=arr.array('i',[12,34,56])
m.append(100)
print(m)
O/P:
array('i', [12, 34, 56, 100])
0 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏