An array is a form of data structure that can store a sequential fixed-size array of the same type of elements. Also, an array in python is used to store a collection of data, but it is often useful to think of an array as a collection of values of the same type. So, arrays are similar to lists but with one difference that we need to have all the values of the same type. For example, if we say it is an int array then all the values stored in the array should be an integer type. If you say it is a float array then all the values stored in the array should be a float. The main advantage of arrays is that they do not have a specific size which means we can expand or shrink it.
Python does not have built-in support for Arrays, but Python Lists can be used instead. To work with arrays in Python we need to import a library and Numpy is one of the libraries used. Here, we will be importing a module called an array.
>>> import array
Instead of using array every time, we can use aliases which is an alternate term for a given data entity which is typically simpler to recognize.
>>> import array as arr
>>> arr.array()
While creating an array we have to specify two things:
- Type
- Values
TYPE
In an array all the values should be of the same data type. If we have an array and its value as an integer we have to mention it is an int array. If it is a float array we have to mention it as a float. Compared with other programming languages int, float keywords can not work in Python.
To mention the type of an array, we have to use Type code which means every type has a unique code. For example, the moment when we change type it will change the size in the memory, for a small integer we use byte, the normal integer will 2 bytes and we have a long integer which will take 4 bytes. Small float will take 4 bytes and double floats will take 8 bytes. For character we have to use U which is Unicode which takes 2 bytes to show data.
Another question may arise: what if you want to have unsigned and signed integers? Signed integers are those types of integer that can have both positive and negative values. If the user wants to store only positive values in that case the user will be going for an unsigned integer because it starts with zero and ends at a particular value. So the advantage of an unsigned integer is that we will not consider negative numbers.
Let’s see the example for creating an array of integer types.
>>> import array as ar
>>> marks = ar.array('i',[20,30,40,60,70])
>>> print ("The newly created array is : ", end =" ")
>>> for i in range (0, 3):
print (marks[i], end =" ")
Output:
The newly created array is: 20 30 40 60 70
Commonly used type codes are listed as follows:
TYPE CODE | C TYPE | PYTHON TYPE | MIN SIZE IN BYTE |
b | Signed Char | int | 1 |
B | Unsigned Char | int | 1 |
u | Py_UNICODE | Unicode | 2 |
h | Signed short | int | 2 |
H | Unsigned short | int | 2 |
i | Signed int | int | 2 |
I (capital i ) | Unsigned int | int | 2 |
l ( small L) | Signed long | int | 4 |
L | Unsigned long | int | 4 |
q | Signed long long | int | 8 |
Q | Unsigned long long | int | 8 |
f | float | float | 4 |
d | double | float | 8 |
Let’s use one function on an array i.e. buffer info which gives the size of an array in tuple form like (a, b) where a is the address of array and size of an array.
>>> from array import *
>>> marks = array('i',[20,30,-40,60,70])
>>> print('Using buffer function -->', marks.buffer_info())
Output:
Using buffer function --> (1970670944664, 5)
APPEND
Arrays are mutable i.e their elements can be changed in a similar way like lists. Using the built-in insert() function we can add elements to the array. As per the requirement, one or more elements can be added at the beginning, end, or any given index of an array. Append() can be used to insert a new element at the end.
>>> import array as ar
>>> a = ar.array('f', [1, 3, 4]) #float array
>>> print ("Array before insertion : ", end =" ")
>>> for i in range (0, 3):
print (a[i], end =" ")
>>> a.insert(1, 2) # inserting 2 at index 1
>>> print ("\n Array after insertion : ", end =" ")
>>> for i in range (0, 4):
print (a[i], end =" ")
Output:
Array before insertion: 1 3 4
Array after insertion : 1 2 3 4
>>> import array as ar
>>> a = ar.array('f', [1,2,3]) #float array
>>> a.insert(4) # inserting 4 at end
>>> print ("\n Array after insertion : ", end =" ")
>>> for i in range (0, 4):
print (a[i], end =" ")
Output:
Array after insertion : 1 2 3 4
ACCESS
In order to access the items of an array in python, we use the index operator [ ] in Python where index must be an integer.
>>> import array as ar
>>> a = ar.array('i', [2, 4, 6, 8])
>>> print("First element:", a[0])
>>> print("Second element:", a[1])
>>> print("Last element:", a[-1])
Output:
First element: 2
Second element: 4
Last element: 8
>>> import array as ar
>>> a = ar.array('f', [1, 2,3, 4]) #float array
>>> print ("Array elements : ", end =" ")
>>> for i in range (len(a)):
print (a[i], end =" ")
Output:
Array elements: 1 2 3 4
The following are the important points to be considered.
- 1. The index starts at 0.
- 2. Array length is 10 which means it can store 10 elements.
CHANGE/UPDATE
>>> import array as ar
>>> a = ar.array('f', [1, 3, 3]) #float array
>>> print ("Array before update : ", end =" ")
>>> for i in range (0, 3):
print (a[i], end =" ")
>>> a[1] = 2 # updating value 2 at index 1
>>> print ("\n Array after update : ", end =" ")
>>> for i in range (0, 3):
print (a[i], end =" ")
Output:
Array before update: 1 3 3
Array after update: 1 2 3
REMOVE
We use remove() and pop() methods to remove items from an array. We can also delete one or more items from an array using Python’s del statement. If we want to remove elements from a specific position of the array, the index of the element is passed as an argument to the pop() method.
>>> import array as ar
>>> number = ar.array('i', [1, 2, 3, 3, 4])
>>> del number[2] # removing third element #output: 1 2 3 4
>>> del number # deleting entire array
>>> number = ar.array('i', [1, 2, 3, 3, 4])
>>> numbers.remove(3) # deletes 3 from first found
>>> print(numbers.pop(2)) # deletes 3 i.e items at index 2
NEW ARRAY WITH SAME VALUE
Here, we are going to create a new array with the same value.
>>> from array import *
>>> marks = array('i',[20,30,-40,60,70])
>>> new_array = array(marks.typecode, (i for i in marks))
>>> new_array
Output:
array('i', [20, 30, -40, 60, 70])
How to create a new array in python with a square of the previous value?
>>> new_array = array(marks.typecode, (i ** 2 for i in marks))
>>> new_array
Output:
array('i', [400, 900, 1600, 3600, 4900])
SEARCH
We use a python in-built index() function to find the index of the first occurrence of value mentioned in arguments.
>>> import array as ar
>>> a = ar.array('i', [4,5,6,7,2,7,4])
>>> print ("The new created array is : ", end ="")
>>> for i in range (len(a)):
print (a[i], end =" ")
>>> print ("The index of the 1st occurrence of 7 is : ", a.index(7))
Output:
The new created array is : 4 5 6 7 2 7 4
The index of the 1st occurrence of 7 is: 3
This page is contributed by Diwas & Sunil. If you like AIHUB and would like to contribute, you can also write an article & mail your article to itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.
This is more helpful info than I’ve found anywhere else.
Any chance you will add some more detail? You’ve come up with a well thought out post anyway, so kudos!
Love to see this every day !
I have been browsing online more than 4 hours today,
yet I never found any interesting article like yours.
It is pretty worth enough for me. In my opinion, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.
When I originally left a comment I appear to have clicked the -Notify me when new comments are
added- checkbox and from now on each time a comment is added I get four emails with the same
comment. There has to be a way you can remove me from that service?
Appreciate it! adreamoftrains web hosting company