An array is a collection of linear data structures that contain all elements of the same data type in contiguous memory space. It is like a container that holds a certain number of elements that have the same data type. An array's index starts at 0, and therefore, the programmer can easily obtain the position of each element and perform various operations on the array. In this section, we will learn about 2D (two dimensional) arrays in Python. Show
Two-Dimensional Array (2D Array)A 2D array is an array of arrays that can be represented in matrix form like rows and columns. In this array, the position of data elements is defined with two indices instead of a single index. Syntax Array_name = [rows][columns] # declaration of 2D array Arr-name = [ [m1, m2, m3, … . mn], [n1, n2, n3, … .. nn] ] Where m is the row and n is the column of the table. Access Two-Dimensional ArrayIn Python, we can access elements of a two-dimensional array using two indices. The first index refers to the indexing of the list and the second index refers to the position of the elements. If we define only one index with an array name, it returns all the elements of 2-dimensional stored in the array. Let's create a simple program to understand 2D (two dimensional) arrays in Python. 2dSimple.py Output: In the above example, we passed 1, 0, and 2 as parameters into 2D array that prints the entire row of the defined index. And we have also passed student_dt[3][4] that represents the 3rd index and 4th position of a 2-dimensional array of elements to print a particular element. Traversing the element in 2D (two dimensional)Program.py Output: Insert elements in a 2D (Two Dimensional) ArrayWe can insert elements into a 2 D array using the insert() function that specifies the element' index number and location to be inserted. Insert.py Output: Update elements in a 2 -D (Two Dimensional) ArrayIn a 2D array, the existing value of the array can be updated with a new value. In this method, we can change the particular value as well as the entire index of the array. Let's understand with an example of a 2D array, as shown below. Create a program to update the existing value of a 2D array in Python. Update.py Output: Delete values from a 2D (two Dimensional) array in PythonIn a 2- D array, we can remove the particular element or entire index of the array using del() function in Python. Let's understand an example to delete an element. Delete.py Output: Size of a 2D arrayA len() function is used to get the length of a two-dimensional array. In other words, we can say that a len() function determines the total index available in 2-dimensional arrays. 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. 2D arrays in PythonWhat 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. Moving with this article on 2D arrays in Python. 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} 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] Moving with this article on 2D arrays in Python. Advantages of using list as an arrayThere are many advantages of using list to describe array. Not only it supports basic operations of array but it has some advance operations too:
Therefore list provide a powerful way to implement arrays. Moving with this article on 2D arrays in Python. 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. 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:
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 Moving with this article on 2D arrays in Python. 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().
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. 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 ) Moving with this article on 2D arrays in Python. 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. array_name = [value1, value2, value3, value n]0
So generalised syntax to update any element of the 2-d array: array_name = [value1, value2, value3, value n]1 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 = [value1, value2, value3, value n]2 For example the student1 marks were wrongly entered and new marks be 45, 78, 96, 78, 85. array_name = [value1, value2, value3, value n]3 array_name = [value1, value2, value3, value n]4 You can see how complete array has been updated. Moving with this article 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: array_name = [value1, value2, value3, value n]5 The generalised syntax being: array_name = [value1, value2, value3, value n]6 However in certain scenarios we have to delete a specific element instead of complete array. We can achieve the same using pop function. array_name = [value1, value2, value3, value n]7 So the generalized syntax for deleting an element of 2-d array will be: array_name = [value1, value2, value3, value n]8 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. |