{"id":8051,"date":"2021-01-28T15:15:29","date_gmt":"2021-01-28T09:45:29","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8051"},"modified":"2025-12-30T04:25:21","modified_gmt":"2025-12-30T09:25:21","slug":"python-lists-and-matrix-with-numpy","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-lists-and-matrix-with-numpy\/","title":{"rendered":"Python Lists and Matrix with Numpy"},"content":{"rendered":"\n<p>In Python, Matrix with NumPy typically handled in two primary ways: by using standard nested lists for basic use cases, or by leveraging the NumPy library for efficient numerical and mathematical computations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Using Basic Python (Nested Lists)<\/strong><\/h2>\n\n\n\n<p>Python does not provide a built-in matrix data type. However, a matrix can be represented as a list of lists, where each inner list corresponds to a row in the matrix.<\/p>\n\n\n\n<p><strong>Matrix creation example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># A 2x3 matrix (2 rows and 3 columns)\nmatrix = [\n    [1, 2, 3],\n    [4, 5, 6]\n]\n<\/pre>\n\n\n\n<p><strong>Accessing elements:<\/strong><br>Elements are accessed using the format <code>matrix[row][column]<\/code>, with indexing starting from zero.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(matrix[0][1])  # Outputs 2 (first row, second column)\n<\/pre>\n\n\n\n<p><strong>Best suited for:<\/strong><br>Simple data representation, small-scale operations, or environments where external libraries cannot be installed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Using NumPy Arrays (Recommended Approach)<\/strong><\/h2>\n\n\n\n<p>For scientific computing and real-world data processing, NumPy is the preferred choice due to its optimized performance and extensive support for linear algebra operations.<\/p>\n\n\n\n<p><strong>Creating a matrix with <code>np.array<\/code>:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\nmatrix = np.array([[1, 2], [3, 4]])\n<\/pre>\n\n\n\n<p>Although NumPy also provides <code>np.matrix<\/code>, this class is now largely deprecated and <code>np.array<\/code> is recommended for all new development.<\/p>\n\n\n\n<p><strong>Common operations in NumPy:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Addition: <code>A + B<\/code><\/li>\n\n\n\n<li>Matrix multiplication: <code>A @ B<\/code> or <code>np.dot(A, B)<\/code><\/li>\n\n\n\n<li>Transpose: <code>matrix.T<\/code><\/li>\n\n\n\n<li>Determinant: <code>np.linalg.det(matrix)<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comparison Overview<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Nested Lists<\/th><th>NumPy Arrays<\/th><\/tr><\/thead><tbody><tr><td>Performance<\/td><td>Slower due to manual iteration<\/td><td>Highly optimized and fast<\/td><\/tr><tr><td>Functionality<\/td><td>Basic data storage<\/td><td>Advanced linear algebra support<\/td><\/tr><tr><td>Syntax<\/td><td>Pure Python<\/td><td>Requires NumPy import<\/td><\/tr><tr><td>Ease of Mathematical Use<\/td><td>Limited and verbose<\/td><td>Designed for mathematical operations<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For more advanced linear algebra tasks, such as matrix inversion or decomposition methods, the <code>scipy.linalg<\/code> module can be used to extend NumPy\u2019s capabilities.<\/p>\n\n\n\n<p>Specifically, by the end of this tutorial, you will learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What matrices are<\/li>\n\n\n\n<li>How to represent matrices in Python&nbsp;<\/li>\n\n\n\n<li>How to access a specific element in a matrix<\/li>\n\n\n\n<li>Addition and subtraction of matrices<\/li>\n\n\n\n<li>Using Numpy for matrix creation and manipulation<\/li>\n\n\n\n<li>Numerical Computation with Numpy arrays<\/li>\n\n\n\n<li>Transposing a matrix with NumPy<\/li>\n\n\n\n<li>Slicing a Matrix with NumPy<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Matrix<\/h2>\n\n\n\n<p>A matrix is a structured way of organizing data into rows and columns. While data can be stored in sets, lists, or other collections, matrices are specifically designed to arrange values in a two-dimensional format. In this structure, rows run horizontally, and columns extend vertically. Understanding this layout is a fundamental concept covered early in any <strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\/\">Python Online Course Certification<\/a><\/strong>, as matrices form the basis for data analysis, numerical computing, and machine learning tasks.<\/p>\n\n\n\n<p>A matrix in mathematics can be defined by its size; the size of the matrix being the number of rows by columns.&nbsp; Below is an example of a matrix.<\/p>\n\n\n\n<p>Since it has 3 rows and 2 columns, it is thus a 3 by 2 matrix.&nbsp;<\/p>\n\n\n\n<p>On row 1, you have values 2, -1<br>On row 2, you have values 0, 5<br>On row 3, you have values 4, 9<br>In column 1, you have values 2, 0, 4<br>In column 2, you have values -1, 5, 9<\/p>\n\n\n\n<p>Let\u2019s see how to represent this in Python.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Representing a Matrix in Python.<\/h2>\n\n\n\n<p>There is no straight way of representing a matrix data type in Python. The closest <a href=\"https:\/\/en.wikipedia.org\/wiki\/Data_type\" rel=\"nofollow noopener\" target=\"_blank\">datatype<\/a> to a matrix is the list data type, and thus, is typically used to create matrices.<\/p>\n\n\n\n<p>However, the Numpy library provides another way of representing matrices in Python, the NumPy array data type. We will discuss both methods in this tutorial.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Introduction to Object-oriented and high-level | Python programming language Training By H2Kinfosys\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/VRnUyzNiLd4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Matrix using Python Lists.&nbsp;<\/h2>\n\n\n\n<p>A list in Python is a collection of homogenous data, encapsulated by square brackets and each separated by a comma. By default, a list is seen as a matrix with one row while the column depends on the number of elements it has. Let\u2019s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#create a one-dimensional list\nlist_1 = [1, -2, 0]<\/pre>\n\n\n\n<p>The list created is a 1 by 3 matrix because it contains 3 elements and it\u2019s a single list. The big question is then, how do we create lists with more than 1 row? Nested lists!<\/p>\n\n\n\n<p>If we wish to create a list with more than one row, we open 2 square brackets and encapsulate each row inside a square bracket. See how it&#8217;s done below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#create a list with more than one row\nlist_1 = [['first row'],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['second row'],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['third row'],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['last row']]<\/pre>\n\n\n\n<p>Let\u2019s now create a 3 by 2 matrix using nested lists.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#create a 3 by 2 list\nlist_1 = [[2, -1],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0, 5],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[4, 9]]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Element in a Python List<\/h2>\n\n\n\n<p>If you wish to access specific elements in a list, you can do so using indexing. To get the entire row, you pass the row index in square brackets. Let\u2019s say we want to access the first row in the list above. We can write.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"647\" height=\"254\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image.png\" alt=\"\" class=\"wp-image-33554\" style=\"aspect-ratio:2.5473392802214705;width:839px;height:auto\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image.png 647w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image-300x118.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image-150x59.png 150w\" sizes=\"(max-width: 647px) 100vw, 647px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">#create a 3 by 2 list\nlist_1 = [[2, -1],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0, 5],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[4, 9]]\n&nbsp;\n#print the first row\nprint(list_1[0])<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>[2, -1]<\/code><\/pre>\n\n\n\n<p>To get a specific element, pass the row and column index in separate square brackets, as in [\u2018row index\u2019][\u2018column index\u2019]. In the above matrix, if we wish to access the element in the first row and second column, we can do it with the following code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#create a 3 by 2 list\nlist_1 = [[2, -1],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0, 5],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[4, 9]]\n&nbsp;\n#print the element in the first row and second column\nprint(list_1[0][1])<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>-1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Matrices in Python<\/h2>\n\n\n\n<p>You can add lists as matrices in Python. See the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#define the two&nbsp; matrices to be added\nlist_1 = [[1, 4, 3],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[10,2,4],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-1,3,2]]\n&nbsp;\nlist_2 = [[3, 6, -6],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,5,-2],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-2,6,3]]\n&nbsp;\n#define a list to store the result\naddtion_result&nbsp; = [[0,0,0],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0,0,0],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0,0,0]]\n&nbsp;\n&nbsp;\n#add the two lists based on their index\nfor i, _ in enumerate(list_1):\n&nbsp;&nbsp;&nbsp;&nbsp;for j, _ in enumerate(list_2):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addtion_result[i][j] = list_1[i][j] + list_2[i][j]\n&nbsp;\n#To Print the matrix\nprint(f\" The addition of list_1 and list_2 is \\n{addtion_result}\")<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;The addition of list_1 and list_2 is&nbsp;<br>[[4, 10, -3], [12, 7, 2], [-3, 9, 5]]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Subtracting of Matrices in Python.&nbsp;<\/h2>\n\n\n\n<p>Just as in addition, you can do an element-based subtraction with nested lists. See an example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#define the two&nbsp; matrices to be added\nlist_1 = [[1, 4, 3],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[10,2,4],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-1,3,2]]\n&nbsp;\nlist_2 = [[3, 6, -6],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,5,-2],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-2,6,3]]\n&nbsp;\n#define a list to store the result\naddtion_result&nbsp; = [[0,0,0],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0,0,0],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[0,0,0]]\n&nbsp;\n&nbsp;\n#multiply the two lists based on their index\nfor i, _ in enumerate(list_1):\n&nbsp;&nbsp;&nbsp;&nbsp;for j, _ in enumerate(list_2):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addtion_result[i][j] = list_1[i][j] - list_2[i][j]\n&nbsp;\n#To Print the matrix\nprint(f\" The subtraction of list_1 and list_2 is \\n{addtion_result}\")<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;The subtraction of list_1 and list_2 is&nbsp;<br>[[-2, -2, 9], [8, -3, 6], [1, -3, -1]]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using NumPy Library for Matrix Creation and Manipulation<\/h2>\n\n\n\n<p>NumPy is a widely used library for performing numerical computations in Python. In fact, the name NumPy is derived from <em>Numerical Python<\/em>. It provides efficient tools for creating and manipulating matrices, which are essential for data processing and mathematical operations. In this section, we will demonstrate how to create matrices and perform computations using NumPy, concepts commonly introduced in an <strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\/\">AI Python Course<\/a><\/strong>. If the library is not already installed on your system, it can be installed by entering the appropriate command.<\/p>\n\n\n\n<p>on your command prompt. It is vital to state that NumPy comes pre-installed if you are using a Jupyter notebook. You would not need to install NumPy again.<\/p>\n\n\n\n<p>Once you have numpy successfully installed, let\u2019s now begin to create matrices with the library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Matrix with Numpy<\/h2>\n\n\n\n<p>In Numpy, matrices are in the form of a NumPy array. Lists are converted to Numpy arrays by calling the array() method and passing the list. Let\u2019s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n#define a one-dimensional array\nmatrix_a = np.array([1, 2, 3])\n&nbsp;\n#print the matrix and its type\nprint(matrix_a)\nprint(type(matrix_a))<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1 2 3]<br>&lt;class 'numpy.ndarray'&gt;<\/pre>\n\n\n\n<p>As seen from the result, the matrix of an object with an ndarray (n-dimensional&nbsp; array) datatype<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Addition of Matrices in Numpy&nbsp;<\/h2>\n\n\n\n<p>The addition of matrices in <a href=\"https:\/\/www.h2kinfosys.com\/blog\/numpy\/\" data-type=\"post\" data-id=\"12794\">NumPy <\/a>is way easier than list addition in the earlier example. After defining the arrays to be added, you simply use the + operator to indicate you want the matrices to be added. See the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\n#define the matrices to be added\nmatrix_1 = np.array([[1, 4, 3],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[10,2,4],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-1,3,2]])\n&nbsp;\nmatrix_2 = np.array([[3, 6, -6],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,5,-2],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-2,6,3]])\n&nbsp;\naddtion_result = matrix_1 + matrix_2\n&nbsp;\n#To Print the matrix\nprint(f\" The addition of list_1 and list_2 is \\n{addtion_result}\")<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>The addition of list_1 and list_2 is&nbsp;<br>[[1, 4, 3], [10, 2, 4], [-1, 3, 2], [3, 6, -6], [2, 5, -2], [-2, 6, 3]]<\/code><\/pre>\n\n\n\n<p>Easy right?&nbsp;<\/p>\n\n\n\n<p>The same thing goes for matrix subtraction<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Matrix Subtraction with Numpy<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\n#define the matrices to be added\nmatrix_1 = np.array([[1, 4, 3],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[10,2,4],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-1,3,2]])\n&nbsp;\nmatrix_2 = np.array([[3, 6, -6],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,5,-2],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-2,6,3]])\n&nbsp;\nsub_result = matrix_1 - matrix_2\n&nbsp;\n#To Print the matrix\nprint(f\" The subtraction of list_2 from list_1 is \\n{sub_result}\")<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>The subtraction of list_2 from list_1 is<br>[[-2 -2 9]<br>[ 8 -3 6]<br>[ 1 -3 -1]]<br>You can also do the matrix multiplication of two matrices.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Multiplying Two Matrices in Numpy<\/h2>\n\n\n\n<p>To multiply two matrices in Numpy, you can use the dot() method of NumPy. See an example<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\n#define the matrices to be added\nmatrix_1 = np.array([[1, 4, 3],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[10,2,4],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-1,3,2]])\n&nbsp;\nmatrix_2 = np.array([[3, 6, -6],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[2,5,-2],&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[-2,6,3]])\n&nbsp;\n#multiply the two matrices\nresult = np.dot(matrix_1, matrix_2)\n&nbsp;\n#To Print the matrix\nprint(f\" The multiplication of list_1 and list_2 is \\n{result}\")<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>The multiplication of list_1 and list_2 is<br>[[ 5 44 -5]<br>[ 26 94 -52]<br>[ -1 21 6]]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Matrix Transpose in NumPy<\/h2>\n\n\n\n<p>Transposing a matrix involves switching the rows to become columns and switching columns to rows as well. To return the transpose of a matrix, the transpose() method can be called<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\n#define the matrix\nmatrix = np.array([[1, 2, 3, 4],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[5, 6, 7, 8,],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[9, 10, 11, 12]])\n&nbsp;\nprint(matrix.transpose())\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>[[ 1 5 9]<br>[ 2 6 10]<br>[ 3 7 11]<br>[ 4 8 12]]<\/code><\/pre>\n\n\n\n<p>Alternatively, you can use the T attribute.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\n#define the matrix\nmatrix = np.array([[1, 2, 3, 4],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[5, 6, 7, 8,],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[9, 10, 11, 12]])\n&nbsp;\nprint(matrix.T)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>[[ 1 5 9]<br>[ 2 6 10]<br>[ 3 7 11]<br>[ 4 8 12]]<\/code><\/pre>\n\n\n\n<p>As seen, it produces the same result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Slice Matrices<\/h2>\n\n\n\n<p>Like in lists, slicing is the process of returning a portion of a matrix. Here are some key things to note about slicing.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When sorting, the start and end index is indicated with the syntax[start: end]<\/li>\n\n\n\n<li>When the start index is not passed, Python takes the start index from the beginning of the list.&nbsp;<\/li>\n\n\n\n<li>When the end index is not passed, Python takes the end index as the end of the list.&nbsp;<\/li>\n\n\n\n<li>When counting from the beginning, the indexing begins from 0, 1, 2, 3\u2026 When counting from the end, the indexing begins from -1, -2, -3, etc.&nbsp;<\/li>\n\n\n\n<li>When the end index is defined, the sliced array returns index minus 1. If say the end index was 3, the slicing stops as the 2nd index<\/li>\n<\/ul>\n\n\n\n<p>Now let&#8217;s apply some of these with concrete examples.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Slicing a One-Dimensional Matrix.<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\n#define the matrix\nmatrix = np.array([1, 2, 3, 4, 5, 6, 7])\n&nbsp;\n#slice the first row\nprint('First row is')\nprint(matrix[:2])\n&nbsp;\n#slice the second column\nprint('Second column is')\nprint(matrix[1:6])\n&nbsp;\n#slice the first row from behind\nprint('First row from to the ')\nprint(matrix[:-1])<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>First row is<br>[1 2]<br>Second column is<br>[2 3 4 5 6]<br>First row from behind is<br>[1 2 3 4 5 6]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Slicing a Multidimensional Array<\/h2>\n\n\n\n<p>When slicing a multidimensional array, the row, and column are separated by a comma.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary library\nimport numpy as np\n&nbsp;\n#define the matrix\nmatrix = np.array([[1, 2, 3, 4],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[5, 6, 7, 8,],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[9, 10, 11, 12]])\n&nbsp;\n#slice the last row\nprint('Last row is')\nprint(matrix[2, :])\n&nbsp;\n#slice the second column\nprint('Second column is')\nprint(matrix[:, -3])\n&nbsp;\n#slie from the second column\nprint('Matrix to the second column ')\nprint(matrix[:, :2])<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Output:<br>Last row is<br>[ 9 10 11 12]<br>Second column is<br>[ 2 6 10]<br>Matrix to the second column<br>[[ 1 2]<br>[ 5 6]<br>[ 9 10]]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Let\u2019s unpack the code.<br>This was the initial matrix that was sliced<br>[[ 1 2 3 4]<br>[ 5 6 7 8]<br>[ 9 10 11 12]]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the first print statement, we sliced [2, :]. The row index was set to 2, while the column was set as the range of all the elements. This is why the elements in the third row were printed. Recall that python indexes from 0. Since index 2 would be the 3rd row.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"805\" height=\"439\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image-1.png\" alt=\"\" class=\"wp-image-33565\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image-1.png 805w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image-1-300x164.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image-1-768x419.png 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/image-1-150x82.png 150w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/figure>\n\n\n\n<p>&nbsp;[ 9 10 11 12]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the next print statement, we sliced [:, -3]. The row index was a range of all numbers, while the column index was set to -3. Recall that when a negative index is passed, Python counts from the end starting from -2. This is why -3 returns the 3rd column from behind or the 2nd index from the beginning.\u00a0<\/li>\n<\/ul>\n\n\n\n<p>[ 2&nbsp; 6 10]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the next print statement, we sliced [:, :2]. The row index was set to print all the rows in the matrix. The second column was set to return a range from the beginning to the second index. This is why the slicing returns the first two columns.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><code>[[ 1&nbsp; 2]<\/code><br><code>[ 5&nbsp; 6]<br>[ 9 10]]<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Let\u2019s conclude.&nbsp;<\/h2>\n\n\n\n<p>In this tutorial, you have discovered how to create matrices using lists and NumPy arrays in Python. You also learned how to do numerical computations on the matrices in Numpy. Worthy of mention is the dot() and transpose() method.\u00a0<\/p>\n\n\n\n<p>Finally, you learned how to slice a specific chunk of matrices by specifying its index or range.\u00a0If you have any questions, feel free to leave them in the comment section and I\u2019d do my best to answer them.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, Matrix with NumPy typically handled in two primary ways: by using standard nested lists for basic use cases, or by leveraging the NumPy library for efficient numerical and mathematical computations. 1. Using Basic Python (Nested Lists) Python does not provide a built-in matrix data type. However, a matrix can be represented as a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8056,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8051"}],"version-history":[{"count":4,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8051\/revisions"}],"predecessor-version":[{"id":33573,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8051\/revisions\/33573"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8056"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}