How To Implement 2-D arrays in Python?

Last updated on Jul 15,2021 16.2K Views

44 / 62 Blog from Python Fundamentals

Welcome everyone to this tutorial on python where we will learn about 2D arrays in Python. The whole article has been divided into several topics with keeping the flow in mind. Following pointers will be covered in this article,

So tighten your seat belts and lets get started with this article on 2D arrays in Python.

What are arrays?

So before diving into array implementation, first lets see what actually are arrays!

Array is a linear data structure that contains ordered collection of elements of the same type in a sequential memory location. So array is a data structure that is used to store elements of same type, linearly in a memory.
In every programming language an array is represented as Array_name[index]. Index is the number that states the location number of a specific element in a memory location. Since it uses sequential memory therefore the index numbers are also continuous. It is quite evident to note that the array indexing starts at 0 and end at n-1 where n is the size of the array.

How are arrays defined and used in python?

So we all know arrays are not present as a separate object in python but we can use list object to define and use it as an array.

For example, consider an array of ten numbers: A = {1,2,3,4,5}
Syntax used to declare an array:

`array_name=[ ]`

If you want to initialize it with values, you can use:

`array_name = [value1, value2, value3, value n]`

To traverse an array we use indexing, for example if we want to get value 2 , we use

`array_name[ location of value 2 starting from 0]`

Advantages of using list as an array

There are many advantages of using list to describe array. Not only it supports basic operations of array but it has some advance operations too:

• It supports slicing. Through slicing we can slice our array in any dimensions.
• It supports negative indexing. Now you can use array_name[ -1 ] to get last element and similarly -2 gives second last element.
• It has built-in functions to store, update and manipulate data.
• It can store di-similar elements together.It has overloaded + and * operator.

Therefore list provide a powerful way to implement arrays.

What is a 2-d array and how to create it using a list object ?

Now here comes the most important part; The 2-d array. From machine learning data frames to matrices, the most widely used array is 2-d array.
Basically a 2-d array is defined as an array of arrays. It may sound a little confusing but let’s just understand what that means.

As of now we have seen an array holding data like [ 1, 2, 3, 4, 5] which can be interpreted as line in coordinate geometry if these points are to be plotted. What if I say I have a data of 5 students for 5 subjects:

• Student 1 = [98, 95, 92, 99, 78]
• Student 2 = [95, 85, 68, 48, 45]
• Student 3 = [98, 95, 92, 99, 25]
• Student 4 = [98, 72, 58, 99, 75]
• Student 5 = [98, 95, 92, 99, 48]

Now you can have five different lists to store this data but this will be inefficient and frankly speaking not a good construct for using list. Moreover, it is even possible for small data but in the case of 100000 students what will we do? Clearly this construct is not acceptable as a good programmer.

Here comes 2-d arrays to the rescue. What if we can store these five arrays of student in an array? Well, we can! This is what array of arrays means. We will store these arrays inside an array itself. Since if you plot these elements considering them geometric points then you will require two planes for it(namely x and y), thus called 2-d array or two dimensional array.

The 2-d array for the same will be:

`student_data = [ [98, 95, 92, 99, 78], [95, 85, 68, 48, 45], [98, 95, 92, 99, 25], [98, 72, 58, 99, 75], [98, 95, 92, 99, 48] ]`

To access the elements we will use two indexes, first index to define the location of list where our element is stored and second index to define the location of element in that list.

For example we want to access the mark of student 3 of subject 4, then we can access it using

`student_data[2][3]`

Note:We have used index no 2 for location 3 because indexing starts at 0

How to insert elements in 2-d array?

Now that we have learnt how to create, initialize and traverse elements of an array of two dimensions. We will now see how the elements are stored in 2-d array.

Consider if we want to store results of student 6 in these 2-d array then we can use in-built function append().

Example

`student6 = [ 56, 89, 48, 96, 45]`

And we want to store it in student_data then,

`student_data.append(student6)`

These will add the elements in last position of the 2-d array. So basically the general syntax is:

`array_name.append(list_name_to_add)`

Suppose student2 has given extra exam of subject6 then his marks has to be included too. So here also we can use the same append function.
But the syntax to insert an element is:

`Array_name[ index_of_sub_array ].append(element_to_add)`

Using the above syntax, If we want to insert 98 marks obtained by student2 then we can use the above syntax as:

`student_data[ 1 ].append( 98 )`

How to update elements of 2-d array?

So till now we have seen how to create our 2-d array, how to initialise it and populate it with data and how to insert elements at any position of the 2-d array. Lets now move on to the next important topic updating elements of 2-d array.

To understand this let’s continue taking the example of student_data.
Now lets say student3 gives reexamination of subject5 and got 98 marks, now this increased marks has to be updated. So how can we achieve this? We can simply achieve this using assignment operator.

`student_data[ 2 ][ 4 ] = 98`

As you can see 25 has been increased to 98.

So generalised syntax to update any element of the 2-d array:

`Array_name[index_of_sub_array][ index_of_element_to_update ] = new_value`

With these you can update elements one by one. But what if you want to update the complete marksheet of student? Then editing each entry is hectic task. So in spite of editing elements one by one we can change entire array of information.

Let’s say student1 data was wrongly entered and now has to be updated, so we can create a new mark_sheet array of student1 and then update corresponding list. It is quite evident to note when we are replacing an array as a whole we don’t require [index_of_element_to_update ] thus the new generalised syntax for updating entire array will be:

`Array_name[index_of_sub_array] = new_sub_array`

For example the student1 marks were wrongly entered and new marks be 45, 78, 96, 78, 85.
Then same can be updated as follows:

`student1 = [45, 78, 96, 78, 85]`
`student_data[ 0 ] = student1`

You can see how complete array has been updated.

How to remove elements of 2-d array?

Now lets see how we can remove any elements of 2-d array. Suppose student4 left the school and his record has to be removed then we will have to remove his entire entry or we can say array. So to remove elements of list object we use pop function.

So to remove the data of student4 we can do as follows:

`student_data.pop( 3 )`

The generalised syntax being:

`array_name.pop(index_of_sub_array_to_remove)`

However in certain scenarios we have to delete a specific element instead of complete array. We can achieve the same using pop function.
Consider student1 unenrolled from subject2 then his entry for subject2 has to be deleted. So to delete subject2 entry of student1,

`student_data[ 0 ].pop(1)`

So the generalized syntax for deleting an element of 2-d array will be:

`Array_name[index_of_sub_array].pop(index_of_element_to_remove)`

In this way you can perform any operations on the 2-d array in python. We started from understanding about arrays then we saw how each operations can be performed efficiently. From creating to updating 2-d arrays, we covered it all. It is Abhishek!, will catch you all again in the next article of this series.

Note:

Indexing starts at 0 not 1

Thus we have come to an end of this article on ‘2D arrays in Python’. With the increasing popularity, the demand has also increased in domains like machine learning, artificial intelligence, data science, etc. To master your skills enroll in Edureka’s Python online course program and kick-start your learning.

Have any questions? Mention them in the comments. We will get back to you as soon as possible.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 28th September,2024

28th September

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 26th October,2024

26th October

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 30th November,2024

30th November

SAT&SUN (Weekend Batch)
View Details