Disadvantages of mongodb LEARNOVITA

What is Python array? Learn with examples

Last updated on 30th Jan 2023, Artciles, Blog

About author

Neel Patel (MongoDB Manager )

Neel Patel is a MongoDB Manager for the Respective Industry and has 6+ years of experience working on JavaScript, HTML, CSS, Ajax, jQuery, MongoDB, ReactJS, and VueJS. His articles help to impart knowledge and skills in core fields and give insightful knowledge to students.

(5.0) | 19867 Ratings 2188
    • In this article you will learn:
    • 1.Why use Arrays in Python?
    • 2.What is an Array?
    • 3.Is an Array the same as a Python list?
    • 4.Creating an Array.
    • 5.Accessing an Element.
    • 6.Adding/ Changing elements of an Array.
    • 7.Concatenation.
    • 8.Looping through an array.
    • 9.Basic Operations.
    • 10.Conclusion.

Why use Arrays in a Python?

When Arrays and Python are used together they can save a lot of time. Arrays help to cut down on the size of a program’s code as a whole and Python unlike the other languages helps to get rid of bad syntax. Now that you know how important arrays are in Python.

What is an Array?

A data structure that can hold more than one value at once is called an array. It is a group or ordered list of things that are all the same type.


  • a=arr.array(‘d’,[1.2,1.3,2.3])

Can easily go through the items in an array and get the values they need by just giving the index number. Arrays are also mutable which means they can be changed and you can do different things with them as needed.

Is a Python list the same as an Array?

In Python values are saved in the same way in both arrays and lists. But there is a big difference in the values that a and b store. Any kind of value like integers strings, etc. can be stored in a list. On the other hand an array only stores one type of data value. So can have an array of numbers an array of strings and so on.Python also has Numpy Arrays which are used in Data Science and are a grid of values. Numpy Arrays vs. Lists is a good place to learn more.

Creating an Array:

After importing the array module you can do the following to make an array in Python:

import array as arr: The array(data type, value list) function needs two things: the data type of the value to store and a list of values to store. The type of data can be any of int, float, double etc. Please keep in mind that arr is an alias name that makes it easier to use. can import even without an alias.

from array import *: This means that you want to use all the functions in the array module.The array is made with the syntax shown below.


  • a=arr.array(data type,value list) #when import using arr alias
  • OR
  • a=array(data type,value list) #when import using *
  • Example:
  • a=arr.array( ‘d’ , [1.1 , 2.1 ,3.1] )

Here the first parameter is “d,” which is the data type (float) and the second parameter is the value.

What is an Array

Accessing array elements :

To get to the elements of an array you need to give an index value. Indexing begins at 0 instead of 1. So an array’s length is always 1 less than its index number.


  • Array_name[index value]
  • Example:
  • a=arr.array( ‘d’, [1.1 , 2.1 ,3.1] )
  • a[1]
  • Output –
  • 2.1
  • The result is a value that is at the second spot in an array and has the value 2.1:

Finding the Length of an Array:

The number of elements that are actually in an array is its length. Can do this with the help of the len() function. The number of elements in an array is returned by the len() function as an integer value.


  • len(array_name)
  • Example:
  • a=arr.array(‘d’, [1.1 , 2.1 ,3.1] )
  • len(a)
  • Output – 3

This returns a result of three which is equal to the number of array elements.

Adding/ Changing elements of an Array:

Use the append(), extend() or insert (i,x) functions to add a value to an array.You use the append() function to add a single item to the end of an array.


  • a=arr.array(‘d’, [1.1 , 2.1 ,3.1] )
  • a.append(3.4)
  • print(a)
  • Output –
  • array(‘d’, [1.1, 2.1, 3.1, 3.4])

The result is a real array to which a new value has been added at the end. You can use the extend() function to add more than one item. This function’s parameter is the list of elements. This list is made up of things that need to be added to an array.


  • a=arr.array(‘d’, [1.1 , 2.1 ,3.1] )
  • a.extend([4.5,6.3,6.8])
  • print(a)
  • Output –
  • array(‘d’, [1.1, 2.1, 3.1, 4.5, 6.3, 6.8])

The 3 new items will be added to the end of the array so they will all be in the final array.

The insert(i,x) function on the other hand can be used to add a specific element to a specific spot in an array. This function adds the given element to the array at the given index. It needs two parameters. The first is the index of the place where the element should go and the second is the value.


  • a=arr.array(‘d’, [1.1 , 2.1 ,3.1] )
  • a.insert(2,3.8)
  • print(a)
  • Output –
  • array(‘d’, [1.1, 2.1, 3.8, 3.1])

The result is an array with the number “3.8” in the third spot.Arrays can also be joined by using the array concatenation method.

Array Concatenation :

Any two arrays can be a concatenated using + symbol.:


  • a=arr.array(‘d’,[1.1 , 2.1 ,3.1,2.6,7.8])
  • b=arr.array(‘d’,[3.7,8.6])
  • c=arr.array(‘d’)
  • c=a+b
  • print(“Array c = “,c)
  • Output :
  • Array c= array(‘d’, [1.1, 2.1, 3.1, 2.6, 7.8, 3.7, 8.6])

The elements of arrays a and b are added together to make array c.

Python array

Looping through an array:

Using a for loop and can loop through an array:


  • a=arr.array(‘d’, [1.1, 2.2, 3.8, 3.1, 3.7, 1.2, 4.6])
  • print(“All values”)
  • for x in a:
  • print(x)
  • print(“specific values”)
  • for x in a[1:3]:
  • print(x)
  • Output –
  • All values
  • 1.1
  • 2.2
  • 3.8
  • 3.1
  • 3.7
  • 1.2
  • 4.6
  • specific values
  • 2.2
  • 3.8

The result of using the for loop is shown above. When the for loop is used without any specific parameters the result is the array’s elements one at a time. In a second for loop the result only has elements whose aindex values were used to select them. Please note that there is no value at index number 3 in result.

Basic Operations:

Traverse means to print each item in an array one at a time:

Insertion- Inserts an element at a certain index.

Deletion − Deletes an element at a given index.

Search- Looks for an element using either an index or a value.

Update − Updates an element at a given index.


Arrays are Data Structures in Python that can hold more than one value of the same type. People often think that they are lists or Numpy Arrays. Arrays in Python are different from both of these in a technical sense.

Are you looking training with Right Jobs?

Contact Us

Popular Courses