{"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":"
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
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>\nOutput :\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>\nConverting 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>\nOutput :\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>\nConverting 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>\nOutput :\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