Sunday 22 July 2018

Array In Python | Traverse, Insert, Update, Delete, Search Array In Python

Arrays In Python | How To Traverse, Insert, Update, Delete, Search Elements In Python


Array - A data structure which contains similar type of elements

elements - Each item in an array is known as Element

Index - Each location in an array has a numerical index which is used to find the element

# Structure of an Array

Elements   [23, 54, 5, 12, 39, 10]
Index           0    1   2   3    4    5

  • By default index start at 0
  • Here length of array is 6, i.e. its storing 6 elements

# Array operations:

Traverse -  print all elements of an array
Insert - Add an element in array at given index
Update - Updates an element in array at given index
Search - Searches an element at given index
Delete - Deletes an element at given index

# Array operations in action:


from array import *

myarray = array('i', [10,20,30,40,50])

print("Traversing the array...")
for x in myarray:
 print(x)

print("Accessing array element...")
print (myarray[0])
print (myarray[2])

print("Inserting element in array...")
myarray.insert(1,60)
for x in myarray:
 print(x)

print("Deleting element in array...")
myarray.remove(40)
for x in myarray:
 print(x)

print("Searching index of an element...")
myarray = array('i', [10,20,30,40,50])
print (myarray.index(40))

print("Update an element in array...")
myarray[2] = 80

for x in myarray:
 print(x)

1 comments:

  1. After examine a few of the weblog posts in your web site now, and I actually like your way of blogging. I bookmarked it to my bookmark web site list and will likely be checking back soon. Pls try my site as properly and let me know what you think. apple hilfe berlin

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel