{"id":3230,"date":"2020-06-17T10:35:38","date_gmt":"2020-06-17T05:05:38","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3230"},"modified":"2026-07-14T19:49:42","modified_gmt":"2026-07-14T14:19:42","slug":"python-2d-list","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-2d-list\/","title":{"rendered":"Python 2D Lists: Create, Index, Copy, Flatten, and Loop"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> A Python 2D list is a list of row lists. Create independent rows with a list comprehension, such as <code>[[0] * columns for _ in range(rows)]<\/code>; avoid <code>[[0] * columns] * rows<\/code> because every row then refers to the same list.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-2d-list-row-safety.png\" alt=\"Python 2D list diagram showing independent row creation, row and column indexing, shallow and deep copy choices\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>Nested lists are flexible, but independent rows and an explicit shape contract make their mutations predictable.<\/figcaption><\/figure>\n<p>A two-dimensional list is a convenient built-in representation for a small table, grid, board, or matrix-like value. The first index selects a row and the second selects a value inside that row. Python lists are mutable, so updating a cell changes the structure in place.<\/p>\n<p>The official <a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html\">Python data structures tutorial<\/a> explains list operations. A 2D list is not a special array type; it is a nested collection, and rows may even have different lengths unless your program enforces a rectangular shape.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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\/#Create_A_2D_List_Safely\" >Create A 2D List Safely<\/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\/#Avoid_Shared_Rows\" >Avoid Shared Rows<\/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\/#Access_Rows_Columns_And_Cells\" >Access Rows, Columns, And Cells<\/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\/#Loop_Through_A_2D_List\" >Loop Through A 2D List<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Copy_A_2D_List_Without_Shared_Rows\" >Copy A 2D List Without Shared Rows<\/a><\/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\/#Flatten_Or_Transpose\" >Flatten Or Transpose<\/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\/#When_To_Use_NumPy\" >When To Use NumPy<\/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\/#Create_An_Empty_2D_List\" >Create An Empty 2D List<\/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\/#Sort_Rows_By_A_Column\" >Sort Rows By A Column<\/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\/#Validate_A_Rectangular_Shape\" >Validate A Rectangular Shape<\/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\/#Choose_A_Data_Structure\" >Choose A Data Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#How_do_I_create_a_2D_list_in_Python\" >How do I create a 2D list in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#Why_is_0_columns_rows_unsafe\" >Why is [[0] * columns] * rows unsafe?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#How_do_I_access_a_value_in_a_Python_2D_list\" >How do I access a value in a Python 2D list?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-2d-list\/#When_should_I_use_NumPy_instead_of_a_2D_list\" >When should I use NumPy instead of a 2D list?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Create_A_2D_List_Safely\"><\/span>Create A 2D List Safely<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-python\">rows = 3\ncolumns = 4\ngrid = [[0] * columns for _ in range(rows)]\n\ngrid[1][2] = 7\nprint(grid)\n<\/code><\/pre>\n<p>The comprehension creates a fresh row for every iteration. This is the important part when each row will be mutated independently.<\/p>\n<p>The same expression can add conditions, but nested syntax becomes dense quickly. Use the evaluation-order map in <a href=\"https:\/\/www.pythonpool.com\/python-list-comprehension-if-else\/\">Python list comprehension with if\/else<\/a> before combining row loops, filters, and conditional values.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Avoid_Shared_Rows\"><\/span>Avoid Shared Rows<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-python\">bad_grid = [[0] * 4] * 3\nbad_grid[0][0] = 9\nprint(bad_grid)\n<\/code><\/pre>\n<p>All rows in <code>bad_grid<\/code> refer to the same inner list, so changing one row changes all three. Check identity with <code>bad_grid[0] is bad_grid[1]<\/code>. The safe comprehension makes that expression false.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/2d-rows-b180.png\" alt=\"Python Pool infographic showing a Python 2D list as rows, columns, and nested lists\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Rows and columns: A Python 2D list as rows, columns, and nested lists.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Access_Rows_Columns_And_Cells\"><\/span>Access Rows, Columns, And Cells<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use two indexes for a cell. Use one index for a complete row.<\/p>\n<pre><code class=\"language-python\">matrix = [\n    [10, 20, 30],\n    [40, 50, 60],\n]\n\n