Arrays in Python

Hello, welcome to today in this article we will learn about the arrays in python which is the most demanding and versatile programming language in the world. So, let’s move on and start with our article.

What is an Arrays in Python Programming?

An array is an object that provides a mechanism for storing several data items with only one identifier, thereby simplifying the task of data management. The array is beneficial if you need to store the group of elements of the same datatype. In Python, arrays can increase or decrease their size dynamically. Arrays are use to organize data, whenever we need more than one variable of the same data type, we mainly use arrays instead of defining each variable. We can say that it’s a collection of variables of the same data type. The array uses less memory than a list.

If you want to store multiple values of the same type then we use arrays. Each item or value is inside an array is called its element. The name of each element inside an array is the same which we defined the name of the array itself but having a unique index number by which we can access each element inside an array. The index of an array is also known as a subscript. Python arrays have different data types.
e.g., int, float etc.

Why we need arrays in Python?

Let’s say in a school there’s are 1000 students and we want to store roll number of each student so what will we do will we make an individual variable for each student roll number. Like below

Example 01:

arrays in python img1


arrays in python img2

No, it will take a lot of time to create 1000 variables and then store values into them, and it’s not an efficient way. And it will need more space to store data.

In this situation, we use arrays in Python. Let’s code the same thing in the array

Example 02:

arrays in python img3


arrays in python img4

You can compare the execution time of both examples.
Example 01 executed in 0.262 seconds.
Example 02 executed in 0.138 seconds.

Different types of Arrays in Python

In python, we can use an array of three different types.

  1. One dimensional array – 1D array – single row multiple columns
  2. Multi-dimensional array – multi-D array – Multiple rows multiple columns
  3. Associative arrays

One Dimensional Array

In this type of array, we contain elements in the only dimensions. I simple words these types of arrays contain one row and multiple columns. The 1D array can be accessed by specifying each element position inside an array by their single index value.

Example 03:

arrays in python img5



Multi-Dimensional Array

In a multi-dimensional array, we have multiple rows and multiple columns, In python, multi-dimensional arrays can be created using a list or NumPy (NumPy is a python library used to create large multi-dimensional arrays and matrices). In a multi-dimensional array, we can use different types of data in a single array.

Multi-dimensional array using lists.

We can create the multi-dimensional array in python using the nested list. Means insert multiple lists in the list.

Example 04:




Example 05:




Associative Array

This is the abstract data type means the associative array is an abstract category of things. For example, “car” is the abstract category of thing, there are different subcategories of car, like Ford or a Ferrari. It contains a collection of (key, value) pairs. No key occurs more than one. You can add pairs to the collection, remove pairs from the collection, modify existing pairs, lookup values associate with a key.

Python has a specific implementation of the associative array which is called a dictionary. In other type arrays, we access the value by its index number but in an associative array we do not access the value by index number but with their associated key. The dictionary contains the collection of pairs each pair is defined with a key on the left. To separate the key from its value we use a colon “:”.

Example 06:




That’s all for this article if you have any queries please contact us through our website or email us at [email protected]

Leave a Comment