In this article we are going to perform array in python with examples, Creating Array in Python, Adding Elements to Array in Python, Updating Elements in Array in Python, Accessing Elements from Array in Python, Slicing of a Array in Python, Removing Elements from Array in Python.
Table of Contents
What is an Array in Python ?
An array is a collection of elements which can stored the elements in memory locations. Array can hold more than one value at a time. And array can hold the same type of items. Array makes easy to calculate the position of items.
#1: Creating Array in Python
To create an array, we need to import array module.
Example of creating array in python:
import array as arr
#creating an array integer type
a = arr.array('i', [1, 3, 5])
print(a)
#creating an array float type
b = arr.array('d', [1.1, 2.2, 3.3])
print(b)
Output:
array('i', [1, 3, 5])
array('d', [1.1, 2.2, 3.3])
Sr. No | Type Code | C Type | Python Type | Min Size in Bytes |
1. | ‘b’ | signed char | int | 1 |
2. | ‘B’ | unsigned char | int | 1 |
3. | ‘u’ | Py_UNICODE | Unicode | 2 |
4. | ‘h’ | signed short | int | 2 |
5. | ‘H’ | unsigned short | int | 2 |
6. | ‘i’ | signed int | int | 2 |
7. | ‘I’ | unsigned int | int | 2 |
8. | ‘l’ | signed long | int | 4 |
9. | ‘L’ | Unsigned long | int | 4 |
10. | ‘f’ | float | float | 4 |
11. | ‘d’ | double | float | 8 |
#2: Adding Elements to Array in Python
In python, we can add one elements to the Array by using built-in append() function.
And add one or more elements to the Array by using extend() function.
Example of Adding Elements to Array in Python:
import array as arr
a = arr.array('i', [6, 7, 8])
# using append() method
a.append(9)
print(a)
# using extend() method
a.extend([3, 4, 5])
print(a)
Output:
array('i', [6, 7, 8, 9])
array('i', [6, 7, 8, 9, 3, 4, 5])
#3: Updating Elements in Array in Python
In Python, Arrays are mutable so we can update the elements in array.
Example of updating elements in Array in Python:
import array as arr
a = arr.array('i', [2, 4, 7, 8, 9, 10])
print(a)
a[0] = 3
print(a)
a[2:4] = arr.array('i', [5, 6])
print(a)
Output:
array('i', [2, 4, 7, 8, 9, 10])
array('i', [3, 4, 7, 8, 9, 10])
array('i', [3, 4, 5, 6, 9, 10])
#4: Accessing Elements from Array in Python
In Python, we use index operator ([]) to access an item in a array.
Example of Accessing elements from array in Python:
import array as arr
a = arr.array('i', [1, 2, 3, 4, 5, 6, 7, 8])
print("Element is:", a[0])
print("Element is:", a[4])
print("Element is:", a[-3])
Output:
Element is: 1
Element is: 5
Element is: 6
#5: Slicing of a Array in Python
In Python, we can access a range of elements in an array by using the slicing operator (:).
Example of slicing of a array in python
import array as arr
a = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
b = arr.array('i', a)
print(b[4:7])
print(b[2:])
print(b[:])
print(b[:-3])
Output:
array('i', [10, 12, 14])
array('i', [6, 8, 10, 12, 14, 16, 18, 20])
array('i', [2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
array('i', [2, 4, 6, 8, 10, 12, 14])
#6: Removing Elements from Array in Python
Using remove() method and
Using pop() method
In Python, Items can be removed from array by using built-in remove() function. Remove() method removes only one element at a time. Pop() method can also used to remove and return elements from array but by default it removes only last element of the array, if remove any specific element from the array, index of the element is passed as an argument to the pop() method.
Example of removing elements from array in python:
import array as arr
a = arr.array('i', [1, 2, 3, 4, 5])
# using remove() method
a.remove(3)
print(a)
# using pop() method
a.pop(1)
print(a)
# using pop() method by default remove last element
a.pop()
print(a)
Output:
array('i', [1, 2, 4, 5])
array('i', [1, 4, 5])
array('i', [1, 4])
Conclusion:
we have covered array in python with examples, Creating Array in Python, Adding Elements to Array in Python, Updating Elements in Array in Python, Accessing Elements from Array in Python, Slicing of a Array in Python, Removing Elements from Array in Python.
Related Articles:
Functions in Python with Examples
Loops in Python 3 with Examples
7 Python Operators with Examples
Python Introduction for Programmers [Part 1]
6 Python Conditional Statements with Examples
classed and objects in python with examples
Reference:
Good work