Python Array with Examples

In this Python tutorial, we will discuss what is an array in Python and how to use lists as an array. We will also create an array by importing an array module in Python, and also we will see some examples.

  • Arrays in python
  • Access elements from Arrays python
  • Python Array update element
  • Length of an Array in python
  • Add Array elements in python
  • Delete Array elements in python
  • Loop in Array elements in python
  • What is an array module in python
  • Create a python Array
  • Accessing Array elements in python
  • Append item in array python
  • Insert element in array python
  • Extend array in python
  • Remove element from an array in python
  • Remove the last element from array python
  • Reverse an array python
  • Python count the occurrence of an element in an array
  • Convert array to list in python
  • Find the index of an element in an array python
  • Python update the element in an array
  • Python lists vs arrays
  • Python list to numpy arrays
  • Python mean of an array
  • Python mean of two arrays
  • Minimum value of array python
  • Maximum value of array python
  • Python minimum value on 2d array
  • Python maximum value on 2d array

Let us try to understand details on Python array with examples.

What is a Python Array?

What is a Python array? A Python array is a collection of items that are used to store multiple values of the same type together.

Example:

food = [fat, protein, vitamin]
print(food)

After writing the above code (arrays in python), Ones you will print ” food ” then the output will appear as “ [“fat”, “protein”, “vitamin”] ”. Here, we created an array containing food names.

You can refer to the below screenshot arrays in python

Arrays in python
Python Array

Access elements from Arrays python

How to access elements from Python Array? In python, to access array items refer to the index number. we will use the index operator ” [] “ for accessing items from the array.

food = ["fat", "protein", "vitamin"]
a = [2]
print(a)

After writing the above code (access elements from arrays python), Ones you will print ” a ” then the output will appear as “ vitamin”. Here, to access elements from arrays we use index number.

You can refer to the below screenshot access elements from arrays python.

Access elements from Arrays python
python access array elements

Python Array update element

How to update an element in Python array? In python, to update elements in an array, we will reassign a new value to the specified index in which we want to update.

food = ["fat", "protein", "vitamin"]
food[0] = "mineral"
print(food)

After writing the above code (python array update element), Ones you will print ” food ” then the output will appear as “ [“mineral”, “protein”, “vitamin”] . Here, we will reassign the new value to the desired index.

You can refer to the below screenshot python array update element

Python Array update element
Python Array update element

Length of an Array in Python

Let us check how to find length of an array in Python? In python, we will use len() method for finding the length of a given array.

food = ["fat", "protein", "vitamin"]
a = len(food)
print(a)

After writing the above code (length of an array in python), Ones you will print ” a ” then the output will appear as “ 3 ”. Here, 3 is the length of an array by using len() method we get the length of an array.

You can refer to the below screenshot length of an array in python

Length of an Array in python
Length of an Array in python

Add Array elements in Python

Now, let us see how to add an element to a Python array? To add elements in a Python array, we can use append() method for adding the elements.

food = ["fat", "protein", "vitamin"]
food.append("mineral")
print(food)

After writing the above code (add array elements in python), Ones you will print ” food ” then the output will appear as “[“fat”, “protein”, “vitamin”, “mineral”] ”. Here, the “mineral” is added to the list.

You can refer to the below screenshot add array elements in python

Add Array elements in python
Python Add an element to an Array

Delete Array elements in Python

How to delete an array elements in Python? In python, to delete array elements from the array we can use pop() method to delete.

food = ["fat", "protein", "vitamin"]
food.pop(0)
print(food)

After writing the above code (delete array elements in python), Ones you will print ” food ” then the output will appear as “[“protein”, “vitamin”] ”. Here, the pop() method will delete “fat” from the array.

You can refer to the below screenshot delete array elements in python

Delete Array elements in python
Delete Array elements in Python

Loop in Array elements in Python

Let us now discuss how to loop through array elements in Python?

In python, the for loop is used to iterate through all the elements present in array.

food = ["fat", "protein", "vitamin"]
for a in food:
print(a)

After writing the above code (loop in array elements in python), Ones you will print ” a ” then the output will appear as ” fat protein vitamin ”. Here, the loop will iterate through all the elements in the array.

You can refer to the below screenshot loop in array elements in python

Loop in Array elements in python
Loop in Array elements in Python

What is an array module in Python

Now, let us understand what is an array module in Python? An array is a data structure that is used to store values of the same data type. To use array in python we need to import the standard array module which gives us an object to denote an array. Here is how you can import an array module in python.

Example:

from array import*

Once you have imported the array module, you can declare an array.

Create a Python Array

Let us now see how to create an array in Python?

We need to import the array module for creating an array of numeric values in python. Here, we created an array of integer type. The letter ‘i’ is used as type code to represent the integer type of array.

Example:

from array import *
a = array('i',[10,11,12,13])
print(a)

After writing the above code (create a python Array), Ones you will print ” a ” then the output will appear as ” array(‘i’, [10, 11, 12, 13]) ”. Here, we created an array of integer type.

You can refer to the below screenshot create a python Array.

Create a python Array
Create a Python Array

Accessing Array elements in Python

To access array items refer to the index number in python. we will use the index for accessing items from the array and the index starts from ‘0’.

Example:

from array import *
a = array('i',[10,11,12,13])
print(a[2])

After writing the above code (accessing Array elements in python), Ones you will print ” a[2] ” then the output will appear as ” 12 ”. Here, to access elements from arrays we use the index number to access elements.

You can refer to the below screenshot accessing Array elements in python.

Accessing Array elements in python
Accessing Array elements in Python

This is how we can access an element from a Python array.

Append item in array Python

Now, let us see how to append an item to a Python array?

To add element in an existing array we can use append() method for adding the elements in python.

Example:

from array import *
a = array('i',[10,11,12,13])
a.append(14)
print(a)

After writing the above code (append item in array python), Ones you will print ” a ” then the output will appear as “ array(‘i’, [10,11,12,13,14]) ”. Here, the item ” 14 ” was appended to the existing array values.

You can refer to the below screenshot append item in array python.

Append item in array python
Append item in array Python

Insert element in array Python

Now, let us see how to insert an element in a Python array? In Python, to insert value at any index of the array we will use the insert() method. Here, the first argument is for the index, and the second argument is for value.

Example:

from array import *
a = array('i',[10,11,12,13])
a.insert(0,9)
print(a)

After writing the above code (insert an element in array python), Ones you will print ” a ” then the output will appear as “ array(‘i’,[9, 10, 11, 12, 13]) ”. Here, the value ” 9 ” is inserted at index ” 0 ” by using the insert() method.

You can refer to the below screenshot insert element in array python.

Insert element in array python
insert an element in a Python array

Extend array in Python

In python, an array value can be extended with more than one value by using extend() method.

Example:

from array import *
my_array1 = array('i',[10,11,12,13])
my_array2 = array('i',[14,15,16,17])
my_array1.extend(my_array2)
print(my_array1)

After writing the above code (extend array in python), Ones you will print ” my_array1 ” then the output will appear as “ array(‘i’,[10, 11, 12, 13, 14, 15, 16, 17]) ”. Here, we are extending more than one value in the array by using the extend() method in python.

You can refer to the below screenshot extend array in python.

Extend array in python
Extend array in Python

Remove element from an array in Python

Now, let us see how to remove an element from an array in Python? In Python, to remove an array element we can use the remove() method and it will remove the specified element from the array.

Example:

from array import *
my_array = array('i',[10,11,12,13])
my_array.remove(12)
print(my_array)

After writing the above code (remove an element from an array in python), Ones you will print ”my_array” then the output will appear as array(‘i’, [10, 11, 13]) ”. Here, the remove() method will remove the “12” from the array.

You can refer to the below screenshot remove an element from an array in python.

Remove element from an array in python
Remove element from an array in Python

Remove the last element from Python array

Let us see how to remove the last element from a Python array? To remove the last element from an array we can use the pop() method and it will remove the last element from the array python.

Example:

from array import *
my_array = array('i',[10,11,12,13])
my_array.pop()
print(my_array)

After writing the above code (remove the last element from array python), Ones you will print ”my_array” then the output will appear as array(‘i’, [10, 11, 12]) ”. Here, the pop() method will pop out the element “13” from the array.

You can refer to the below screenshot remove the last element from array python.

Remove the last element from array python
Remove the last element from Python array

Reverse an array in Python

Let us discuss how to reverse an array in Python? In python, to reverse an array we will use the reverse() method and it will reverse all the element present in array.

Example:

from array import*
my_array = array('i',[10,11,12,13])
my_array.reverse()
print(my_array)

After writing the above code (reverse an array python), Ones you will print ”my_array” then the output will appear as array(‘i’, [13, 12, 11, 10]) ”. Here, the reverse() method will reverse the element of an array.

You can refer to the below screenshot reverse an array python.

Reverse an array python
Reverse an array in Python

Python count occurrence of an element in an array

How to count the occurrence of an element in a Python array? To count the occurrence of an element we will use count() and it will return the number of times an element appears in an array.

Example:

from array import*
my_array = array('i',[10,11,12,13,12])
a = my_array.count(12)
print(a)

After writing the above code (python count the occurrence of an element in an array), Ones you will print ”a” then the output will appear as 2 ”. Here, the count() method will return the number of times the element appears, and here ’12’ occurred ‘2’ times.

You can refer to the below screenshot python count the occurrence of an element in an array.

Python count the occurrence of an element in an array
Python count occurrence of an element in an array

Convert array to list in Python

How to convert array to a list in Python? To convert array to list in Python, we will use tolist() method and it will convert the array to list object.

Example:

from array import*
my_array = array('i',[10,11,12,13])
a = my_array.tolist()
print(a)

After writing the above code (convert array to list in python), Ones you will print ”a” then the output will appear as [10, 11, 12, 13] ”. Here, the tolist() method will convert my array to a list.

You can refer to the below screenshot convert array to list in python.

Convert array to list in python
Convert array to list in Python

Find index of an element in an array Python

How to Find the index of an element in a Python array? To find the index of an element we will use the python in-built index() method to search the index of a specified value from the array.

Example:

from array import*
my_array = array('i',[10,11,12,13])
print(my_array.index(12)

After writing the above code (find the index of an element in an array python), Ones you will print ”my_array.index(12)” then the output will appear as 2 ”. Here, the index() method will return the index of that specified value.

You can refer to the below screenshot find the index of an element in an array python.

Find the index of an element in an array python
Find index of an element in an array Python

Python update the element in an array

How to update an array element in Python? In python, to update the element in the array at the given index we will reassign a new value to the specified index which we want to update, and the for loop is used to iterate the value.

Example:

from array import*
my_array = array('i',[10,11,12,13])
my_array[1] = 20
for a in my_array:
print(a)

After writing the above code (python update the element in an array), Ones you will print ”a” then the output will appear as 10 20 12 13 ”. Here, we reassign the new value to the index ‘1’ and it is updated with value ’20’ in an array.

You can refer to the below screenshot python update the element in an array.

Python update the element in an array
Python update the element in an array

Python lists vs arrays

ListsArrays
Python lists are flexible and can hold arbitrary data.Python arrays are just a thin wrapper on C arrays.
The list is part of python, so they do not need to be declared first.Arrays need to be imported or declared first from other libraries(i.e. numpy).
It cannot directly handle arithmetic operations.Can directly handle arithmetic operations.
Preferred for shorter sequences of data items.Preferred for longer sequences of data items.
Greater flexibility allows easy modification of data.Less flexibility since addition, deletion has to be done element-wise.
Consume larger memory for easy addition of elements.Comparatively more compact in memory size.

Python list to numpy arrays

To convert the list to an array we first need to import numpy. Then we will initialize a list and to convert the list to an array we will use numpy.array().

Example:

import numpy
my_lst = [10, 12, 16, 18, 20]
my_array = numpy.array(my_lst)
print ("Array:", my_array)
print (type(my_array))

After writing the above code (python list to numpy arrays), Ones you will print ”my_array” then the output will appear as “ Array: [10 12 16 18 20] ”. Here, the numpy.array() takes the list as an argument and returns an array that contains all the elements of the list.

You can refer to the below screenshot python list to numpy arrays.

Python list to numpy arrays
Python list to numpy arrays

Python mean of an array

Now, let us see mean of an array in python.

We will use numpy.mean() for the given data of an array. The function is used to compute the arithmetic mean. The function will return the average of the array element.

Example:

import numpy as np
my_arr = [12, 4, 2, 7]
print("array:", my_arr)
print("Mean of an array:",np.mean(my_arr))

After writing the above code (python mean of an array), Ones you will print ”np.mean(my_array)” then the output will appear as “ array: [12, 4, 2, 7] Mean of an array: 6.25”. Here, the numpy.mean(my_arr) takes the array and returns the mean of the array.

You can refer to the below screenshot python mean of an array.

Python mean of an array
Python mean of an array

Python mean of two arrays

We will use numpy.mean() for the given data of two arrays. The function is used to compute the arithmetic mean. The function will return the average of the two arrays element.

Example:

import numpy as np
my_arr = np.array([[10, 2], [5, 11]])
a = np.mean(my_arr)
print(a)

After writing the above code (python mean of two arrays), Ones you will print ”np.mean(my_arr)” then the output will appear as “ 7.0 ”. Here, the numpy.mean(my_arr) takes the array and returns the mean of the two arrays. The arithmetic mean is the sum of the element divided by the number of elements.

You can refer to the below screenshot for python mean of two array.

Python mean of two arrays
Python mean of two arrays

Minimum value of array python

First, we will import numpy, and then we will create an array. To find the minimum value from the array we will use the “numpy.min(my_arr)” function.

Example:

import numpy 
my_arr = numpy.array([1, 3, 5, 2, 8, 10]) 
min_element = numpy.min(my_arr) 
print('Minimum element in the array is: ',min_element)

After writing the above code (minimum value of array python), Ones you will print “min_element” then the output will appear as “Minimum element in the array is: 1”. Here, the numpy.min(my_arr) will return the minimum value from the array.

You can refer to the below screenshot for minimum value of array python.

Minimum value of array python
Minimum value of array python

Maximum value of array python

First, we will import numpy, and then we will create an array. To find the maximum value from the array we will use the “numpy.max(my_arr)” function.

Example:

import numpy 
my_arr = numpy.array([1, 3, 5, 2, 8, 10]) 
max_element = numpy.max(my_arr) 
print('Maximum element in the array is: ',max_element)

After writing the above code (maximum value of array python), Ones you will print “max_element” then the output will appear as “Maximum element in the array is: 10”. Here, the numpy.max(my_arr) will return the maximum value from the array.

You can refer to the below screenshot for maximum value of array python.

Maximum value of array python
Maximum value of array python

Python minimum value on 2d array

First, we will import numpy, and then we will create a 2d array. To find the minimum value from the two-dimensional array we will use the “numpy.min(my_arr)” function.

Example:

import numpy 
my_arr = numpy.array([[15, 12, 3], [14, 25, 20], [9, 60, 80]]) 
min_element = numpy.min(my_arr) 
print('Minimum element on 2d array is:',min_element)

After writing the above code (python minimum value on 2d array), Ones you will print “min_element” then the output will appear as “Minimum element on 2d array is: 3”. Here, the numpy.min(my_arr) will return the minimum value from the 2d array.

You can refer to the below screenshot for python minimum value on 2d array.

Python minimum value on 2d array
Python minimum value on 2d array

Python maximum value on 2d array

First, we will import numpy, and then we will create a 2d array. To find the maximum value from the two-dimensional array we will use the “numpy.max(my_arr)” function.

Example:

import numpy 
my_arr = numpy.array([[15, 12, 3], [14, 25, 20], [9, 60, 80]]) 
max_element = numpy.max(my_arr) 
print('Maximum element on 2d array is:',max_element)

After writing the above code (python maximum value on 2d array), Ones you will print “max_element” then the output will appear as “Maximum element on 2d array is: 80”. Here, the numpy.max(my_arr) will return the maximum value from the 2d array.

You can refer to the below screenshot for python maximum value on 2d array

Python maximum value on 2d array
Python maximum value on 2d array

You may like the following Python tutorials:

In this Python tutorial, we learned about python arrays and also how to use it like:

  • What is an Array in Python
  • Access elements from Arrays python
  • How to update am element in Python Array
  • How to get Length of an Array in python
  • How to Add Array elements in python
  • Delete Array elements in python
  • Loop in Array elements in python
  • What is an array module in python
  • Create a python Array
  • Accessing Array elements in python
  • Append item in array python
  • How to Insert element in array python
  • Extend array in python
  • Remove element from an array in python
  • How to remove the last element from a Python array
  • Reverse an array python
  • Python count the occurrence of an element in an array
  • Convert array to list in python
  • Find the index of an element in an array python
  • How to update the element in an array in Python
  • Python lists vs arrays
  • Python list to numpy arrays
  • Python mean of an array
  • Python mean of two arrays
  • Minimum value of array python
  • Maximum value of array python
  • Python minimum value on 2d array
  • Python maximum value on 2d array