Python Arrays

Python Arrays

Python does not provide an in-built data type of Arrays. It provides lists though. A list can be considered as an array. An array is a type of container which is used to store a number of elements in it and are accessed by something called indexing. It implies that an array element can be accessed by specifying an index number which is basically its position inside the array.

 

 

Create Arrays in Python

An array which is nothing but a list can be created by separating the elements by a comma and placing them inside a pair of square brackets.

Example:

 

# Python program to exemplify array creation

info = ['Kenna', 'Scotty', 'Ledger']  
print(info)

Output:

['Kenna', 'Scotty', 'Ledger']

 

 

Access Element of an Array

To access an element from an array, it’s index number is used. It starts with 0 from forward direction and -1 from backward direction.

Example:

 

# Python program to exemplify accessing elements of an array 

info = ['Kenna', 'Scotty', 'Ledger']  
print(info[0])
print(info[1])
print(info[2])
print(info[-1])
print(info[-2])
print(info[-3])

Output:

Kenna
Scotty
Ledger
Ledger
Scotty
Kenna

 

 

Access Range of Elements of an Array

To access range of elements of an array, statement like [first_index:last_index] is used which is called as slicing where first_index is included and last_index is not. If none of these are mentioned inside the square brackets with a colon, then they take the first and last index of the array respectively.

Example:

 

# Python program to exemplify accessing range of elements of an array 

info = ['Kenna', 'Scotty', 'Ledger', 'Nicole', 'Skylar', 'Jude']
print(info[1:4])
print(info[:-1])
print(info[3:])
print(info[2:-2])

Output:

['Scotty', 'Ledger', 'Nicole']
['Kenna', 'Scotty', 'Ledger', 'Nicole', 'Skylar']
['Nicole', 'Skylar', 'Jude']
['Ledger', 'Nicole']

 

 

Modify Value of an Element

To modify value of an element, access is using it’s index number and then assign it a new value using an assignment operator.

Example:

 

# Python program to exemplify modification of an element of an array 

info = ['Kenna', 'Scotty', 'Ledger', 'Nicole', 'Skylar', 'Jude']
print(info[0])
info[0]='Megan'
print(info[0])

Output:

Kenna
Megan

 

 

Add Elements in an Array

Two methods can be used to add elements in an array and they are defined as follows:

  • append() – This method adds an element to the end of the array.
  • insert() – This method inserts an element at the specified index in the array.

Example:

# Python program to exemplify addition of elements in an array 

info = ['Kenna', 'Scotty', 'Ledger', 'Nicole', 'Skylar', 'Jude']

# adds 'Kenzi' at the end
info.append('Kenzi')
print(info)

# adds 'Toren' at the index 2 and shifts the element to the right
info.insert(2,'Toren')
print(info)

Output:

['Kenna', 'Scotty', 'Ledger', 'Nicole', 'Skylar', 'Jude', 'Kenzi']
['Kenna', 'Scotty', 'Toren', 'Ledger', 'Nicole', 'Skylar', 'Jude', 'Kenzi']

 

 

Delete Elements of an Array

There are 4 ways using which elements of an array can be deleted and they are defined as follows:

  • remove() – This method deletes first occurrence of the specified element from the array
  • pop() – This method deletes the element at the specified index. If no index is passed to the method, then last element from the array is deleted.
  • clear() – This method deletes all the elements from the array.
  • del – It deletes an element or a range of elements from the array.

Example:

 

# Python program to exemplify deletion of elements of an array 

info = ['Kenna', 'Scotty', 'Ledger', 'Nicole', 'Skylar', 'Jude']

info.remove('Scotty')
print(info)

info.pop(3)
print(info)

info.pop()
print(info)

del info[2]
print(info)

info.clear()
print(info)

Output:

['Kenna', 'Ledger', 'Nicole', 'Skylar', 'Jude']
['Kenna', 'Ledger', 'Nicole', 'Jude']
['Kenna', 'Ledger', 'Nicole']
['Kenna', 'Ledger']
[]

 

 

Length of Python Arrays

To find length of an array, len() function is used.

Example:

 

# Python program to exemplify len() function

info = ['Kenna', 'Scotty', 'Ledger', 'Nicole', 'Skylar', 'Jude']
print(len(info))

Output:

6

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *