{"id":4801,"date":"2023-10-26T15:53:04","date_gmt":"2023-10-26T10:23:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=4801"},"modified":"2023-11-10T12:00:30","modified_gmt":"2023-11-10T06:30:30","slug":"convert-2d-numpy-array-to-list-of-lists-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/","title":{"rendered":"Convert 2D NumPy array to list of lists in python"},"content":{"rendered":"

How to convert 2D NumPy array to list of lists in python ?<\/h2>\n

In this article we will discuss about different ways of converting NumPy array to list of lists in Python. So let’s start exploring the topic.<\/p>\n

Converting a 2D Numpy Array to list of lists using tolist() :<\/h3>\n

In NumPy module of Python, there is a member function tolist() which returns a list congaing the elements of the array. If the array is a 2D array then it returns lists of list.<\/p>\n

So, let’s see the implementation of it.<\/p>\n

#Program :\r\n\r\nimport numpy as np\r\n# 2D Numpy array created\r\narr = np.array([[11, 22, 33, 44],\r\n                [55, 66, 77, 88],\r\n                [12, 13, 23, 43]])\r\n\r\n#printing the 2D array                \r\nprint(arr)\r\n# Converting 2D Numpy Array to list of lists\r\nlist_of_lists = arr.tolist()\r\n#Printing the list of lists\r\nprint(\"The list is\")\r\nprint(list_of_lists)<\/pre>\n
Output :\r\n[[11, 22, 33, 44], \r\n[55, 66, 77, 88], \r\n[12, 13, 23, 43]]\r\nThe list is\r\n[[11, 22, 33, 44], [55, 66, 77, 88], [12, 13, 23, 43]]<\/pre>\n

Converting a 2D Numpy array to list of lists using iteration :<\/h3>\n

We can iterate a 2D array row by row and during iteration we can add it to the list. And at the end we can get the list of lists containing all the elements from 2D numpy array.<\/p>\n

#Program :\r\n\r\nimport numpy as np\r\n# 2D Numpy array created\r\narr = np.array([[11, 22, 33, 44],\r\n                [55, 66, 77, 88],\r\n                [12, 13, 23, 43]])\r\n\r\n#printing the 2D array                \r\nprint(arr)\r\n# Converting a 2D Numpy Array to list of lists\r\n#iterating row by row using for loop\r\nlist_of_lists = list()\r\nfor row in arr:\r\n    list_of_lists.append(row.tolist())\r\n#Printing the list of lists\r\nprint(\"The list is\")\r\nprint(list_of_lists)<\/pre>\n
Output :\r\n[[11, 22, 33, 44], \r\n[55, 66, 77, 88], \r\n[12, 13, 23, 43]]\r\nThe list is\r\n[[11, 22, 33, 44], [55, 66, 77, 88], [12, 13, 23, 43]]<\/pre>\n

Converting a 2D Numpy Array to a flat list :<\/h3>\n

In both the example, we observed that a 2D NumPy array converted into list of lists. But we can also convert it into a\u00a0 flat list (not a list of lists) . So, for that first we can convert the 2D NumPy array into 1D Numpy array by using flatten()<\/code> method.. Then call the tolist()<\/code> function to convert it into flat list.<\/p>\n

#Program :\r\n\r\nimport numpy as np\r\n# 2D Numpy array created\r\narr = np.array([[11, 22, 33, 44],\r\n                [55, 66, 77, 88],\r\n                [12, 13, 23, 43]])\r\n\r\n#printing the 2D array                \r\nprint(arr)\r\n\r\n# Converting 2D Numpy array to a flat list\r\nmy_list = arr.flatten().tolist()\r\n#Printing the list of lists\r\nprint(\"The list is\")\r\nprint(my_list)<\/pre>\n
Output :\r\n[[11, 22, 33, 44], \r\n[55, 66, 77, 88], \r\n[12, 13, 23, 43]] \r\nThe list is [11, 22, 33, 44, 55, 66, 77, 88, 12, 13, 23, 43]<\/pre>\n","protected":false},"excerpt":{"rendered":"

How to convert 2D NumPy array to list of lists in python ? In this article we will discuss about different ways of converting NumPy array to list of lists in Python. So let’s start exploring the topic. Converting a 2D Numpy Array to list of lists using tolist() : In NumPy module of Python, …<\/p>\n

Convert 2D NumPy array to list of lists in python<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nConvert 2D NumPy array to list of lists in python - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert 2D NumPy array to list of lists in python - Python Programs\" \/>\n<meta property=\"og:description\" content=\"How to convert 2D NumPy array to list of lists in python ? In this article we will discuss about different ways of converting NumPy array to list of lists in Python. So let’s start exploring the topic. Converting a 2D Numpy Array to list of lists using tolist() : In NumPy module of Python, … Convert 2D NumPy array to list of lists in python Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-26T10:23:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:30:30+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#webpage\",\"url\":\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/\",\"name\":\"Convert 2D NumPy array to list of lists in python - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-26T10:23:04+00:00\",\"dateModified\":\"2023-11-10T06:30:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Convert 2D NumPy array to list of lists in python\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Convert 2D NumPy array to list of lists in python\",\"datePublished\":\"2023-10-26T10:23:04+00:00\",\"dateModified\":\"2023-11-10T06:30:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#webpage\"},\"wordCount\":216,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Convert 2D NumPy array to list of lists in python - Python Programs","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:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Convert 2D NumPy array to list of lists in python - Python Programs","og_description":"How to convert 2D NumPy array to list of lists in python ? In this article we will discuss about different ways of converting NumPy array to list of lists in Python. So let’s start exploring the topic. Converting a 2D Numpy Array to list of lists using tolist() : In NumPy module of Python, … Convert 2D NumPy array to list of lists in python Read More »","og_url":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-26T10:23:04+00:00","article_modified_time":"2023-11-10T06:30:30+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#webpage","url":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/","name":"Convert 2D NumPy array to list of lists in python - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-26T10:23:04+00:00","dateModified":"2023-11-10T06:30:30+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Convert 2D NumPy array to list of lists in python"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Convert 2D NumPy array to list of lists in python","datePublished":"2023-10-26T10:23:04+00:00","dateModified":"2023-11-10T06:30:30+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#webpage"},"wordCount":216,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/convert-2d-numpy-array-to-list-of-lists-in-python\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4801"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=4801"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4801\/revisions"}],"predecessor-version":[{"id":4807,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4801\/revisions\/4807"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=4801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=4801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=4801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}