In this article, we will learn how to convert 2D Numpy Array to a nested list i.e. list of lists.
Convert 2D Numpy Array to list of lists using tolist()
In Python’s numpy module, the ndarray class provides a member function tolist(), which returns a list containing the copy of elements in the numpy array. If numpy array is 2D, then it returns a list of lists. For example,
import numpy as np # Create 2D Numpy Array arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [3, 3, 3, 3]]) print(arr) # Convert 2D Numpy Array to list of lists list_of_lists = arr.tolist() print(list_of_lists)
Output:
[[1 2 3 4] [5 6 7 8] [3 3 3 3]] [[1, 2, 3, 4], [5, 6, 7, 8], [3, 3, 3, 3]]
It returned a list of lists with the copy of elements in the two dimensional numpy array.
Convert 2D Numpy array to list of lists using iteration
Create an empty list and iterate over all the rows in 2D numpy array one by one. For each of the row, we can add it to the list as a sub list. At the end of the iteration, we will have a list of lists containing all the elements from 2D numpy array. For example,
import numpy as np # Create 2D Numpy array filled with 0's arr = np.zeros( (4, 5), dtype=np.int64) print(arr) # Convert 2D Numpy Array to list of lists list_of_lists = list() for row in arr: list_of_lists.append(row.tolist()) print(list_of_lists)
Output:
[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Convert 2D Numpy Array to a flat list
In both the previous solutions, we converted the 2D Numpy array to a nested list i.e. list of lists. What if we want to convert 2D array to a flat list ? For that we need to first flatten the 2D numpy array to a 1D numpy array and then call tolist() function on that. For example,
import numpy as np # Create 2D Numpy array filled with 0's arr = np.zeros( (4, 5), dtype=np.int64) print(arr) # Convert 2D Numpy array to a flat list num_list = arr.flatten().tolist() print(num_list)
Output:
[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Summary:
In this article we learned about different ways to convert a 2D Numpy array to either list of lists or a flat list in python
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.