Array in Python with Examples

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.

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])
Array in Python with Examples 1
Sr. NoType CodeC TypePython TypeMin Size in Bytes
1.‘b’signed charint1
2.‘B’unsigned charint1
3.‘u’Py_UNICODEUnicode2
4.‘h’signed shortint2
5.‘H’unsigned shortint2
6.‘i’signed intint2
7.‘I’unsigned intint2
8.‘l’signed longint4
9.‘L’Unsigned longint4
10.‘f’floatfloat4
11.‘d’doublefloat8

#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])
Array in Python with Examples 2

#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])
Array in Python with Examples 3

#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
Array in Python with Examples 4

#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])
Array in Python with Examples 5

#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])
Array in Python with Examples 6

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:

Python Official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern at startup Product Company. Likes to share knowledge.

1 thought on “Array in Python with Examples”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link