Introduction to Arrays in Python
An array is a collection of homogeneous items where each item has its own index value, and the index starts from 0. Python doesn’t have built-in support for Arrays, but we can import array and use them. There is another datatype similar to arrays in Python, i.e., Lists which are useful as arrays in Python but are different in a way that lists can hold any type of values, but Arrays store only similar type of values, another lists are built-in datatype in Python whereas, Arrays you have to import from array module.
- Index: is the number representing a value in the array and always start with 0.
- element: is the value in an array.
- len(): is the total count of elements in an array.
- append(): This is the method to add an element to the array.
- remove(): is the method to remove an element from the array.
There are many methods similar to append and remove to help us perform various operations on the array or lists. The for-in loop structure is used to loop through the array in Python. Let us learn this as well.
How does Array Work in Python?
The array is stored in contiguous memory locations, where the index is a sequence of numbers that represents the values stored at every particular index. To access or refer to the value at a particular index in an array, we make use of a set of square brackets [ ]; also, we can use the for-in loop to iterate through the array. The array has indices (plural form of an index) and values. At each index, a value is stored. We use arrays because it is difficult to store and remember hundreds of numbers at a time; it would be simpler and easier to use arrays in that case, say the integer array is like the following. array (‘i’, [1, 2 , 3, 4 , 5, 6, 7, 8, 9,10] ) then to access these values we will use the following format.
a[0] => 1
a[1] => 2
a[2] => 3
a[3] => 4
a[4] => 5
a[5] => 6
a[6] => 7
a[7] => 8
a[8] => 9
a[9] => 10
Remember, the index starts at 0. We will use a for-in loop also to loop through the given array:
for i in a
print a[i]
Which will give us the values from 1 to 10.
How to Create Arrays in Python?
To create an array in Python, we need to import the array module first.
import array as arr
where,
arr => is an alias
The other way to import the module is in the following manner:
from array import *
The syntax to create an array is:
array(typecode [,intializer])
where,
typecode => int or float or double or the type of value the array holds.
initializer => is the optional value and can be of any type like list, string, or any iterable elements of a particular type.
The typecode with its value is mentioned below in a tabular format.
TypeCode | C Type | Python Type | Value |
i | signed int | int | 2 |
I | Unsigned int | long | 2 |
b | signed char | int | 1 |
B | Unsigned char | int | 1 |
h | signed short | int | 2 |
H | Unsigned short | int | 2 |
l | signed long | int | 4 |
L | Unsigned long | int | 4 |
f | float | float | 8 |
d | double | float | 4 |
Let us go through the following examples to understand each of these.
Typecode: i
Code:
import array as arr
a=arr.array('i', [10 , 20 ,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: I
Code:
import array as arr
a=arr.array('I', [10 , 20 ,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: b
Code:
import array as arr
a=arr.array('b', [10 , 20 ,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: B
Code:
import array as arr
a=arr.array('B', [10 , 20 ,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: h
Code:
import array as arr
a=arr.array('h', [10,20,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: H
Code:
import array as arr
a=arr.array('H', [10,20,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: l
Code:
import array as arr
a=arr.array('l', [10,20,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: L
Code:
import array as arr
a=arr.array('L', [10,20,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: f
Code:
import array as arr
a=arr.array('f', [10,20,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Typecode: d
Code:
import array as arr
a=arr.array('d', [10,20,30] )
print("Element at 0th index: " , a[0])
print("Element at 1st index: " , a[1])
print("Element at 2nd index: " , a[2])
Output:
Array Methods in Python
We will see the following methods on the given array:
1. typecode()
Syntax:
array.typecode()
This function returns the value of typecode used in the given array.
Code:
#importing array module
import array as arr
#creating array
a1 = arr.array('i', [100,200,300] )
#printing array with method
print(a1.typecode)
Output:
2. insert()
Syntax:
array.insert(index, element)
It adds an element to the array before the index
Code:
#importing array module
import array as arr
#creating array
a = arr.array('i', [100,200,300])
#inserting a value of 400 to after index 2
a.insert(3, 400);
#looping through array a
for i in a:
print(i)
Output:
3. update()
Syntax:
arrayname[index] = value
It updates a particular value at an index to the new value.
Code:
#importing array module
import array as arr
#creating array
a = arr.array('i', [100,200,300] )
#updating a value of 200 to 400
a[1] = 400
#looping through array a
for i in a:
print(i)
Output:
4. delete()
Syntax:
array.remove(element)
This function removes the element from the array.
Code:
#importing array module
import array as arr
#creating array
a = arr.array('i', [100,200,300] )
#deleting a value 100
a.remove(100)
#looping through array a
for i in a:
print(i)
Output:
5. append()
Syntax:
array.append(element)
This function appends the element to the end of the array.
Code :
#importing array module
import array as arr
#creating array
a = arr.array('i', [100,200,300] )
#appending 400 to the end
a.append(400)
#printing array
for i in a:
print(i)
Output:
6. reverse()
Syntax :
array.reverse()
This function reverses the order of elements in the given array.
Code:
#importing array module
import array as arr
#creating array
a = arr.array('i', [100,200,300] )
//applying the reverse method to the array
a.reverse()
//printing the array
for i in a:
print(i)
Output:
7. count()
Syntax:
array.count(element)
This function returns how many times the element occurred in the given array.
Code:
#importing array module
import array as arr
#creating array
a3 = arr.array('i', [100,200,300,100,400,100,500] )
#printing the array count
print(a3.count(100))
Output:
8. index()
Syntax:
array.index(x)
This method returns “I”, which is the index and the smallest value of the first occurrence of x in the array.
Code:
#importing array module
import array as arr
#creating array
a3 = arr.array('i', [700,200,300,100,400,100,500] )
#search the value 100 and return its index
print(a3.index(100))
Output:
9. pop()
Syntax:
array.pop([ i ])
This function removes and returns the element that has an index I of the given array. By default, it removes and returns the last element.
Code:
#importing array module
import array as arr
#creating array
a3 = arr.array('i', [100,200,300] )
# removing 100 and printing
print(a3.pop(0))
Output:
10. itemsize()
Syntax:
array.itemsize()
Code:
#importing array module
import array as arr
#creating array
a3 = arr.array('i', [100,200,300] )
#printing the itemsize
print(a3.itemsize)
Output:
11. len() method
Syntax:
len(arrayname)
This method gives the array length.
Code:
#importing array module
import array as arr
#creating array
a3 = arr.array('i', [100,200,300,400,500] )
#printing the length of method
print(len(a3))
Output:
Conclusion
I hope this article is informative for all of your concepts and also hope this article is interesting enough to encourage you to learn new things. Practice will only lead you to perfection, so practice as much as possible.
Recommended Article
This is a guide to the Arrays in Python. Here we discuss How to Create Arrays in Python and its Methods along with Code implementation and Output. You can also go through our related articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses