{"id":3230,"date":"2020-06-17T10:35:38","date_gmt":"2020-06-17T05:05:38","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3230"},"modified":"2024-01-01T14:57:29","modified_gmt":"2024-01-01T09:27:29","slug":"python-2d-list","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-2d-list\/","title":{"rendered":"Python 2d List: From Basic to Advance"},"content":{"rendered":"\n<p>The <strong>list<\/strong> is one of the most useful data-type in <strong>python<\/strong>. We can add values of all types like integers, string, float in a single list. List initialization can be done using square brackets [].&nbsp;&nbsp;Below is an example of a 1d list and 2d list. As we cannot use 1d list in every use case so <strong>python 2d list<\/strong> is used. Also, known as lists inside a list or a nested list. The number of elements in a 2d list will be equal to the no. of row * no. of columns. <strong>A 2d list looks something like this.&nbsp;<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist1_d=&#x5B;\u2019sonu', 'ashu\u2019, 50, 10.1] \nlist_2d=&#x5B; &#x5B;5,6,7,7] , &#x5B;5,4,6,7] , &#x5B;9,8,9,10] ] \n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"708\" height=\"436\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-12.png\" alt=\"python 2d list\" class=\"wp-image-3234\" style=\"width:416px;height:256px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-12.png 708w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-12-300x185.png 300w\" sizes=\"(max-width: 708px) 100vw, 708px\" \/><\/figure><\/div>\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Graphical_Representation\" >Graphical Representation &nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#How_to_declareinitialize_a_2d_python_list\" >How to declare\/initialize a 2d python list&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#How_to_Retrieve_values_from_a_python_2d_list\" >How to Retrieve values from a python 2d list<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Limitations_of_2d_list\" >Limitations of 2d list<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#i\" >&nbsp;<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Advantages_of_using_2d_list\" >Advantages of using 2d list:&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#How_to_apply_some_common_methods_on_2d_python_list\" >How to apply some common methods on 2d python list:&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Python_sort_2d_list_by_Column\" >Python sort 2d list by Column<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Python_2d_list_to_1d_using_flatten\" >Python 2d list to 1d using flatten()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#FAQs\" >FAQs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Must_Read\" >Must Read:<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-graphical-representation\"><span class=\"ez-toc-section\" id=\"Graphical_Representation\"><\/span>Graphical Representation &nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Graphically a 2d list can be represented in the form of a grid.&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"333\" height=\"263\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-10.png\" alt=\"python 2d list\" class=\"wp-image-3232\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-10.png 333w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-10-300x237.png 300w\" sizes=\"(max-width: 333px) 100vw, 333px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-declare-initialize-a-2d-python-list\"><span class=\"ez-toc-section\" id=\"How_to_declareinitialize_a_2d_python_list\"><\/span>How to declare\/initialize a 2d python list&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are multiple ways to initialize a 2d list in python.&nbsp; &nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>This is the basic approach for creating a 2d list in python.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nrows=&#x5B;] \ncolumns=&#x5B;] \nfor i in range(3):\n   for j in range(3):\n      columns.append(1)\n   rows.append(columns)\n<\/pre><\/div>\n\n\n<p>OUTPUT-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1, 1, 1], [1, 1, 1], [1, 1, 1]]<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"796\" height=\"628\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-13.png\" alt=\"python 2d list\" class=\"wp-image-3235\" style=\"width:395px;height:311px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-13.png 796w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-13-300x237.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-13-768x606.png 768w\" sizes=\"(max-width: 796px) 100vw, 796px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Suppose you want to create a 2d list with coordinates as its values.&nbsp;<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = &#x5B;&#x5B;(i,j) for j in range(3)] for i in range(3)]  \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">[[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)]] <\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"984\" height=\"532\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-14.png\" alt=\"python 2d list\" class=\"wp-image-3236\" style=\"width:479px;height:258px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-14.png 984w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-14-300x162.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-14-768x415.png 768w\" sizes=\"(max-width: 984px) 100vw, 984px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>If we want to create a 2d list with 0 as its value:&nbsp;<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = &#x5B;&#x5B;0 for j in range(3)] for i in range(3)] \n<\/pre><\/div>\n\n\n<p id=\"block-7ab68ee5-4870-4a88-9de8-6147d4b3512c\">Output-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[ [0, 0, 0], [0, 0, 0], [0, 0, 0] ] <\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"972\" height=\"436\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-15.png\" alt=\"python 2d list\" class=\"wp-image-3237\" style=\"width:515px;height:231px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-15.png 972w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-15-300x135.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-15-768x344.png 768w\" sizes=\"(max-width: 972px) 100vw, 972px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-retrieve-values-from-a-python-2d-list\"><span class=\"ez-toc-section\" id=\"How_to_Retrieve_values_from_a_python_2d_list\"><\/span>How to Retrieve values from a python 2d list<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A 2d list can be accessed by two attributes namely row and columns and to  access these list we use the following code-&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor i in range(len(rows)):\n   for j in range(len(columns)):\n      print(rows&#x5B;i]&#x5B;j],end=&quot;&quot;)\nprint(&quot;\\n&quot;)\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"816\" height=\"436\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-16.png\" alt=\"python 2d list\" class=\"wp-image-3238\" style=\"width:425px;height:226px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-16.png 816w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-16-300x160.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-16-768x410.png 768w\" sizes=\"(max-width: 816px) 100vw, 816px\" \/><\/figure><\/div>\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1 1 1\n1 1 1\n1 1 1<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"640\" height=\"292\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-17.png\" alt=\"Retrieve values from a python 2d list\u00a0\u00a0\" class=\"wp-image-3239\" style=\"width:380px;height:173px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-17.png 640w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-17-300x137.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure><\/div>\n\n\n<p>We can also use the concept of slicing for retrieving the data from whichever rows and columns we want.&nbsp; <\/p>\n\n\n\n<p>Suppose that we only want to retrieve row 1 and 2 and all the columns, for this purpose we can use slicing.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist1 = &#x5B; &#x5B;1,2,3], &#x5B;4,5,6], &#x5B;7,8,9], &#x5B;10,11,12] ]\nprint(list1&#x5B;1:3]&#x5B;:]) &quot;&quot;&quot; &#x5B;1:3] is used for rows-1,2&#x5B;:] is used to represent all values&quot;&quot;&quot;\n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"816\" height=\"580\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-19.png\" alt=\"Retrieve values from a python 2d list\u00a0\u00a0\" class=\"wp-image-3241\" style=\"width:456px;height:324px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-19.png 816w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-19-300x213.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-19-768x546.png 768w\" sizes=\"(max-width: 816px) 100vw, 816px\" \/><\/figure><\/div>\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[4, 5, 6], [7, 8, 9]]&nbsp;<\/pre>\n\n\n\n<p>Now, let suppose we want to retrieve all the rows but only column 1:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(list1&#x5B;:]&#x5B;1])\n<\/pre><\/div>\n\n\n<p id=\"block-e2b71bb8-fb98-4a7f-aefd-28dd2e9009a7\">Output-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[4,5,6]&nbsp;<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"908\" height=\"292\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-20.png\" alt=\"Retrieve values from a python 2d list\u00a0by column 1\" class=\"wp-image-3242\" style=\"width:492px;height:158px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-20.png 908w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-20-300x96.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-20-768x247.png 768w\" sizes=\"(max-width: 908px) 100vw, 908px\" \/><\/figure><\/div>\n\n\n<p>But this is not columns 1. Why? It&#8217;s because it is the limitation of a python 2d list that we cannot perform column-wise operations on a 2d list. For that purpose, we have a NumPy array.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-limitations-of-2d-list\"><span class=\"ez-toc-section\" id=\"Limitations_of_2d_list\"><\/span>Limitations of 2d list<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As seen in the last example we cannot perform the column-wise operation in a 2d list. For this purpose, we have to use a 2d NumPy array. To convert a 2d list into a 2d array we first have to import the NumPy library using pip install NumPy and then do the following operations:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist_2d=&#x5B;&#x5B;1,2,3] ,&#x5B;4,5,6] ,&#x5B;7,8,9],&#x5B;10,11,12]] \nimport numpy #importing numpy library  \narr_2d=numpy.array(list_2d) #array function converts list into array \nprint(type(list1)) \nprint(type(arr_2d))\n<\/pre><\/div>\n\n\n<p id=\"block-3aa291d6-6a6d-45bc-a748-04c527f33434\">Output&nbsp;&#8211;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;class 'list'&gt;&nbsp;\n&lt;class 'numpy.ndarray'&gt;&nbsp;<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"804\" height=\"772\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-21.png\" alt=\"python 2d list\" class=\"wp-image-3243\" style=\"width:489px;height:469px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-21.png 804w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-21-300x288.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-21-768x737.png 768w\" sizes=\"(max-width: 804px) 100vw, 804px\" \/><\/figure><\/div>\n\n\n<p>And now if we try the same way to find the 1st column of all the rows we are getting the correct answer with the 2d array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(list_2d&#x5B;:]&#x5B;1]) # Incorrect Output- &#x5B;4, 5, 6] \nprint(arr_2d&#x5B;:,1]) # Correct Output -&#x5B; 2 5 8 11] \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-\"><span class=\"ez-toc-section\" id=\"i\"><\/span>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"828\" height=\"436\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-22.png\" alt=\"python 2d list\" class=\"wp-image-3244\" style=\"width:527px;height:277px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-22.png 828w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-22-300x158.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-22-768x404.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-advantages-of-using-2d-list\"><span class=\"ez-toc-section\" id=\"Advantages_of_using_2d_list\"><\/span>Advantages of using 2d list:&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We cannot simply use a 1d list for every purpose. Suppose we have data of different items purchased by different customers and we want to store those items in a list.&nbsp;<\/p>\n\n\n\n<p>One way to do that is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncustomer_1=&#x5B;&quot;rice\u201d, \u201dcumin seeds\u201d, \u201dwheat\u201d] \ncustomer_2=&#x5B;&quot;detergent\u201d, \u201dsoap\u201d, \u201dshampoo\u201d] \ncustomer_3=&#x5B;&quot;bread\u201d, \u201deggs\u201d, \u201dsalt\u201d] \n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"962\" height=\"436\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-23.png\" alt=\"Advantages of using 2d list\" class=\"wp-image-3245\" style=\"width:550px;height:248px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-23.png 962w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-23-300x136.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-23-768x348.png 768w\" sizes=\"(max-width: 962px) 100vw, 962px\" \/><\/figure><\/div>\n\n\n<p>But let suppose we have 1000 customers or maybe more, will it be the right way, to make 1000 different lists?&nbsp;<\/p>\n\n\n\n<p>But if we use 2d array to represent the items, it would be a lot better, cleaner approach, and a faster one too.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nitems=&#x5B; &#x5B;&quot;rice\u201d,\u201dcumin seeds\u201d,\u201dwheat\u201d] , &#x5B;&quot;detergent\u201d,\u201dsoap\u201d,\u201dshampoo\u201d] , &#x5B;&quot;bread\u201d,\u201deggs\u201d,\u201dsalt\u201d] ] \n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"828\" height=\"340\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-24.png\" alt=\"python 2d list\" class=\"wp-image-3246\" style=\"width:566px;height:232px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-24.png 828w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-24-300x123.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-24-768x315.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/figure><\/div>\n\n\n<p>Also, it would be very easy to retrieve the values from the list. We will learn how to retrieve values from a 2d list in python.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-apply-some-common-methods-on-2d-python-list\"><span class=\"ez-toc-section\" id=\"How_to_apply_some_common_methods_on_2d_python_list\"><\/span>How to apply some common methods on 2d python list:&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>append(): append() is quite a common function used on a list. It is used to add value at the last of the list.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>If we want to add a new list at the end of the old list,&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist_2d= &#x5B;&#x5B;1,2,3] ,&#x5B;4,5,6],&#x5B;7,8,9],&#x5B;10,11,12]] \nlist_2d.append(&#x5B;5,6,7]) \nprint(list_2d) \n<\/pre><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"948\" height=\"340\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-25.png\" alt=\"python 2d list\" class=\"wp-image-3247\" style=\"width:549px;height:197px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-25.png 948w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-25-300x108.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-25-768x275.png 768w\" sizes=\"(max-width: 948px) 100vw, 948px\" \/><\/figure><\/div>\n\n\n<p>OUTPUT:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [5,6,7,]]&nbsp;<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"940\" height=\"244\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-26.png\" alt=\"add a new list at the end of the old list\" class=\"wp-image-3248\" style=\"width:553px;height:143px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-26.png 940w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-26-300x78.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-26-768x199.png 768w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><\/figure><\/div>\n\n\n<p>If we want to add an element at the last of a sub-list:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(list_2d&#x5B;0].append(5))\n<\/pre><\/div>\n\n\n<p id=\"block-a9da70cb-9c61-4e90-bd12-84e6808bb4b7\">Output-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1, 2, 3 ,5], [4, 5, 6], [7, 8, 9], [10, 11, 12], [5,6,7,]]&nbsp;<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"946\" height=\"436\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-27.png\" alt=\"python 2d list\" class=\"wp-image-3249\" style=\"width:536px;height:247px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-27.png 946w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-27-300x138.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-27-768x354.png 768w\" sizes=\"(max-width: 946px) 100vw, 946px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>sort(): If we want to arrange them in ascending or descending order we use sort().&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Now lets see what happens when we apply <a href=\"https:\/\/en.wikipedia.org\/wiki\/Sorting_algorithm\" target=\"_blank\" rel=\"noreferrer noopener\">sort()<\/a> on a 2d list.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist_2d=&#x5B;&#x5B;7, 8, 9],&#x5B; 11,10, 12], &#x5B;4, 5, 6], &#x5B;1, 2, 3]] \nlist_2d.sort() \nlist_2d # By default it gives output in ascending order\n<\/pre><\/div>\n\n\n<p id=\"block-826d0512-c291-48b1-8036-d99f3436636b\">OUTPUT:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1, 2, 3], [4, 5, 6], [7, 8, 9], [11, 10, 12]]&nbsp;&nbsp;<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"772\" height=\"724\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-28.png\" alt=\"python 2d list\" class=\"wp-image-3250\" style=\"width:467px;height:437px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-28.png 772w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-28-300x281.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-28-768x720.png 768w\" sizes=\"(max-width: 772px) 100vw, 772px\" \/><\/figure><\/div>\n\n\n<p>Observe that in the output only the list as a whole is sorted and not the sub list.&nbsp;<\/p>\n\n\n\n<p>If we want to sort both sub-list and the outer list, we can use the following code,&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist_2d=&#x5B;&#x5B;7, 8, 9],&#x5B; 11,10, 12], &#x5B;4, 5, 6], &#x5B;1, 2, 3]]\nfor i in range(len(list_2d)):\n   list_2d&#x5B;i].sort() #sorting the sublist\nlist_2d.sort()\nprint(list_2d)\n<\/pre><\/div>\n\n\n<p id=\"block-f3f761bc-4e55-4060-af3b-52c9f92c4320\">OUTPUT-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]&nbsp;<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"740\" height=\"676\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-29.png\" alt=\"python 2d list\" class=\"wp-image-3251\" style=\"width:470px;height:429px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-29.png 740w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-29-300x274.png 300w\" sizes=\"(max-width: 740px) 100vw, 740px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Flattening the 2d list: When we convert a 2d list into a 1d list, the process is known as flattening. In python so many methods and libraries can help us in achieving the goal, we can also first convert the 2d list into a 2d array and then apply flat() on the array. But as we are in the learning phase we will use the na\u00efve approach to achieve the same.&nbsp;<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist_2d=&#x5B;&#x5B;7, 8, 9],&#x5B;11,10, 12],&#x5B;4, 5, 6],&#x5B;1, 2, 3]] \nlist1=&#x5B;] \nfor i in range(len(list_2d)): \n   for j in range(len(list_2d&#x5B;i])): \n       list1.append(list_2d&#x5B;i]&#x5B;j])\n<\/pre><\/div>\n\n\n<p id=\"block-fbf5cfac-9e76-40e7-95a0-c3d34f78f56d\">OUTPUT-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[7, 8, 9, 11, 10, 12, 4, 5, 6, 1, 2, 3]&nbsp;<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img decoding=\"async\" width=\"826\" height=\"724\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-30.png\" alt=\"python 2d list\" class=\"wp-image-3252\" style=\"width:511px;height:447px\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-30.png 826w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-30-300x263.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-30-768x673.png 768w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_sort_2d_list_by_Column\"><\/span>Python sort 2d list by Column<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In order to sort a 2d list , we will use the sort() function. This will return a tuple of the required output. Besides this, lambda works on the specified elements one by one. So it will help you fetch the result.&nbsp; In this code, n means which row you want to sort.&nbsp;<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n2darr.sort(key=lambda row: (row&#x5B;n]))\n<\/pre><\/div>\n\n\n<p>If you don&#8217;t want the original 2d array to change, use the sorted() function instead of sort().&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_2d_list_to_1d_using_flatten\"><\/span>Python 2d list to 1d using flatten()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Using the np.flatten() method makes the job fairly easy. Just provide a 2d array to this function and it will provide us with a new 1d array.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr = np.array(&#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6], &#x5B;7, 2, 3]])\n\nprint(str(arr))\n\nAns = ini_array1.flatten()\n\nprint(&quot;1d Array output&quot;, Ans)\n<\/pre><\/div>\n\n\n<p>Besides this, you can also opt for np.ravel() function.&nbsp; It works in a similar manner.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1683094474921\"><strong class=\"schema-faq-question\">How do you compare two 2D lists in python?<\/strong> <p class=\"schema-faq-answer\">Compare function can be used like:<br\/>def compare(l1,l2):\u00a0<br\/>  return l1 == l2<br\/>Or to use equality operator,==, the order should be same. Else, the result won&#8217;t be correct.\u00a0<\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span>Must Read:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"http:\/\/www.pythonpool.com\/python-lowercase\/\">How to Convert String to Lowercase in<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/square-root-in-python\/\">How to Calculate Square Root<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\">User Input | Input () Function | Keyboard Input<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\">Best Book to Learn Python<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>Python 2d list<\/strong> has a lot of advantages and limitations too. We should always know when to use it. We should know when to use dictionaries, NumPy array, python 1d list.&nbsp; &nbsp;<\/p>\n\n\n\n<p>Try to run the programs on your side and let me know if you have any queries.<\/p>\n\n\n\n<p><strong><em>Happy Coding!<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The list is one of the most useful data-type in python. We can add values of all types like integers, string, float in a single &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python 2d List: From Basic to Advance\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#more-3230\" aria-label=\"More on Python 2d List: From Basic to Advance\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":3261,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[995,1005,1004,991,998,1010,1012,1009,994,1008,1013,996,997,1001,993,1018,1003,1015,1011,1007,1000,992,1002,999,1014,1006,990,1017,1016],"class_list":["post-3230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-2d-list-comprehension-python","tag-2d-list-in-python","tag-2d-list-python","tag-convert-list-to-2d-array-python","tag-create-2d-list-python","tag-create-a-2d-list-python","tag-create-empty-2d-list-python","tag-creating-a-2d-list-in-python","tag-flatten-2d-list-python","tag-how-to-create-2d-list-in-python","tag-how-to-create-a-2d-list-in-python","tag-how-to-make-a-2d-list-in-python","tag-initialize-2d-list-python","tag-python-2d-list","tag-python-2d-list-comprehension","tag-python-2d-list-to-1d","tag-python-append-to-2d-list","tag-python-convert-2d-list-to-numpy-array","tag-python-create-2d-list","tag-python-create-empty-2d-list","tag-python-empty-2d-list","tag-python-flatten-2d-list","tag-python-initialize-2d-list","tag-python-print-2d-list","tag-python-slice-2d-list","tag-python-sort-2d-list","tag-python-transpose-2d-list","tag-sort-2d-list-python","tag-transpose-2d-list-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python 2d List: From Basic to Advance - Python Pool<\/title>\n<meta name=\"description\" content=\"The Python 2d list is one of the most useful data-type. We can add values of all types like integers, string, float in a single list. Example of 2d list\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python 2d List: From Basic to Advance\" \/>\n<meta property=\"og:description\" content=\"The list is one of the most useful data-type in python. We can add values of all types like integers, string, float in a single list. List initialization\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-2d-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-17T05:05:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-01T09:27:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1001\" \/>\n\t<meta property=\"og:image:height\" content=\"604\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwini Mandani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"Python 2d List: From Basic to Advance\",\"datePublished\":\"2020-06-17T05:05:38+00:00\",\"dateModified\":\"2024-01-01T09:27:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/\"},\"wordCount\":986,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg\",\"keywords\":[\"2d list comprehension python\",\"2d list in python\",\"2d list python\",\"convert list to 2d array python\",\"create 2d list python\",\"create a 2d list python\",\"create empty 2d list python\",\"creating a 2d list in python\",\"flatten 2d list python\",\"how to create 2d list in python\",\"how to create a 2d list in python\",\"how to make a 2d list in python\",\"initialize 2d list python\",\"python 2d list\",\"python 2d list comprehension\",\"python 2d list to 1d\",\"python append to 2d list\",\"python convert 2d list to numpy array\",\"python create 2d list\",\"python create empty 2d list\",\"python empty 2d list\",\"python flatten 2d list\",\"python initialize 2d list\",\"python print 2d list\",\"python slice 2d list\",\"python sort 2d list\",\"python transpose 2d list\",\"sort 2d list python\",\"transpose 2d list python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-2d-list\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-2d-list\/\",\"name\":\"Python 2d List: From Basic to Advance - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg\",\"datePublished\":\"2020-06-17T05:05:38+00:00\",\"dateModified\":\"2024-01-01T09:27:29+00:00\",\"description\":\"The Python 2d list is one of the most useful data-type. We can add values of all types like integers, string, float in a single list. Example of 2d list\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#faq-question-1683094474921\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-2d-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg\",\"width\":1001,\"height\":604,\"caption\":\"Python 2d List\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python 2d List: From Basic to Advance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#faq-question-1683094474921\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/python-2d-list\/#faq-question-1683094474921\",\"name\":\"How do you compare two 2D lists in python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Compare function can be used like:<br\/>def compare(l1,l2):\u00a0<br\/>  return l1 == l2<br\/>Or to use equality operator,==, the order should be same. Else, the result won't be correct.\u00a0\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python 2d List: From Basic to Advance - Python Pool","description":"The Python 2d list is one of the most useful data-type. We can add values of all types like integers, string, float in a single list. Example of 2d list","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-2d-list\/","og_locale":"en_US","og_type":"article","og_title":"Python 2d List: From Basic to Advance","og_description":"The list is one of the most useful data-type in python. We can add values of all types like integers, string, float in a single list. List initialization","og_url":"https:\/\/www.pythonpool.com\/python-2d-list\/","og_site_name":"Python Pool","article_published_time":"2020-06-17T05:05:38+00:00","article_modified_time":"2024-01-01T09:27:29+00:00","og_image":[{"width":1001,"height":604,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg","type":"image\/jpeg"}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"Python 2d List: From Basic to Advance","datePublished":"2020-06-17T05:05:38+00:00","dateModified":"2024-01-01T09:27:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/"},"wordCount":986,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg","keywords":["2d list comprehension python","2d list in python","2d list python","convert list to 2d array python","create 2d list python","create a 2d list python","create empty 2d list python","creating a 2d list in python","flatten 2d list python","how to create 2d list in python","how to create a 2d list in python","how to make a 2d list in python","initialize 2d list python","python 2d list","python 2d list comprehension","python 2d list to 1d","python append to 2d list","python convert 2d list to numpy array","python create 2d list","python create empty 2d list","python empty 2d list","python flatten 2d list","python initialize 2d list","python print 2d list","python slice 2d list","python sort 2d list","python transpose 2d list","sort 2d list python","transpose 2d list python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-2d-list\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/","url":"https:\/\/www.pythonpool.com\/python-2d-list\/","name":"Python 2d List: From Basic to Advance - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg","datePublished":"2020-06-17T05:05:38+00:00","dateModified":"2024-01-01T09:27:29+00:00","description":"The Python 2d list is one of the most useful data-type. We can add values of all types like integers, string, float in a single list. Example of 2d list","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#faq-question-1683094474921"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-2d-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-1.jpg","width":1001,"height":604,"caption":"Python 2d List"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python 2d List: From Basic to Advance"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-2d-list\/#faq-question-1683094474921","position":1,"url":"https:\/\/www.pythonpool.com\/python-2d-list\/#faq-question-1683094474921","name":"How do you compare two 2D lists in python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Compare function can be used like:<br\/>def compare(l1,l2):\u00a0<br\/>  return l1 == l2<br\/>Or to use equality operator,==, the order should be same. Else, the result won't be correct.\u00a0","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3230"}],"version-history":[{"count":11,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3230\/revisions"}],"predecessor-version":[{"id":31575,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3230\/revisions\/31575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/3261"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}