In this post, I am going to write about "arrays". You will learn, what an array is, what the difference is between an array and a list, how do we declare an array, how to add, insert, and element, how to extend an array and how to remove elements and some type codes.
Prerequisites:
2- Lists
Array: Array is a data structure that can hold many data inside like lists. The difference is that arrays can only store the same kind of data. To declare that the data structure is an array, we use the "array" command. The array is not a built-in function in Python. Therefore, we need to import it before we use it. I am sure you all remember how to import a module or library. Even if not, you will see it in the next example.
Code:
>>>from array import * #importing array library
>>>x = array('i', [1,2,3,4]) # creating an array.
>>>print(type(x)) # printing the type of "x"
Output:
<class 'array.array'>
As you can see, when we created an array, we used a typecode. The list of typcodes and explanations are like this:
- ‘b’ -> Represents signed integer of size 1 byte
- ‘B’ -> Represents unsigned integer of size 1 byte
- ‘c’ -> Represents character of size 1 byte
- ‘u’ -> Represents unicode character of size 2 bytes
- ‘h’ -> Represents signed integer of size 2 bytes
- ‘H’ -> Represents unsigned integer of size 2 bytes
- ‘i’ -> Represents signed integer of size 2 bytes
- ‘I’ -> Represents unsigned integer of size 2 bytes
- ‘w’ -> Represents unicode character of size 4 bytes
- ‘l’ -> Represents signed integer of size 4 bytes
- ‘L’ -> Represents unsigned integer of size 4 bytes
- ‘f’ -> Represents floating point of size 4 bytes
- ‘d’ -> Represents floating point of size 8 bytes
Same ss lists, we use indexing numbers to call an element inside the array.
Code:
>>>from array import *
>>>x = array('i', [1,2,3,4])
>>>print((x[0]))
Output:
1
We can append new elements to the array by using "append()".
Code:
>>>from array import *
>>>x = array('i', [1,2,3,4])
>>>print((x))
>>>x.append(5)
>>>print((x))
Output:
array('i', [1, 2, 3, 4])
array('i', [1, 2, 3, 4, 5])
We can insert new element to the array by using "insert()".
Code:
>>>from array import *
>>>x = array('i', [1,2,3,4])
>>>print((x))
>>>x.insert(4,5) #first argument (4) is used for referencing index valure, second argument (5) is the value we insert to the array.
>>>print((x))
Output:
array('i', [1, 2, 3, 4])
array('i', [1, 2, 3, 4, 5])
We can extend the array by using "extend()".
Code:
>>>from array import *
>>>x = array('i', [1,2,3,4])
>>>print((x))
>>>y = array('i', [5,6,7,8,9])
>>>x.extend(y)
>>>print((x))
Output:
array('i', [1, 2, 3, 4])
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])
We can remove an element from the array by using "remove()".
Code:
>>>from array import *
>>>x = array('i', [1,2,3,4])
>>>print((x))
>>>x.remove(3) # In paranthases, I used the element to remove. It is not an index number.
>>>print((x))
Output:
array('i', [1, 2, 3, 4])
array('i', [1, 2, 4])
We can remove the last item from the array by using "pop()".
Code:
>>>from array import *
>>>x = array('i', [1,2,3,4])
>>>print((x))
>>>x.pop()
>>>print((x))
Output:
array('i', [1, 2, 3, 4])
array('i', [1, 2, 3])
or any element at given index by the same command.
Code:
>>>from array import *
>>>x = array('i', [1,2,3,4])
>>>print((x))
>>>x.pop(0) #Here, I am removing the element at index "0".
>>>print((x))
Output:
array('i', [1, 2, 3, 4])
array('i', [2, 3, 4])