Prerequisite: Python List, Numpy ndarray
Both lists and NumPy arrays are inter-convertible. Since NumPy is a fast (High-performance) Python library for performing mathematical operations so it is preferred to work on NumPy arrays rather than nested lists.
Method 1: Using numpy.array().
Approach :
- Import numpy package.
- Initialize the nested list and then use numpy.array() function to convert the list to an array and store it in a different object.
- Display both list and NumPy array and observe the difference.
Below is the implementation.
Python3
# importing numpy library import numpy # initilizing list ls = [[1, 7, 0], [ 6, 2, 5]] # converting list to array ar = numpy.array(ls) # displaying list print ( ls) # displaying array print ( ar) |
Output :
[[1, 7, 0], [6, 2, 5]] [[1 7 0] [6 2 5]]
Method 2: Using numpy.asarray().
Approach :
- Import numpy package.
- Initialize the nested 4-dimensional list and then use numpy.asarray() function to convert the list to the array and store it in a different object.
- Display both list and NumPy array and observe the difference.
Below is the implementation.
Python3
# importing numpy library import numpy # initilizing list ls = [[1, 7, 0],[ 6, 2, 5],[ 7, 8, 9],[ 41, 10, 20]] # converting list to array ar = numpy.asarray(ls) # displaying list print ( ls) # displaying array print ( ar) |
Output :
[[1, 7, 0], [6, 2, 5], [7, 8, 9], [41, 10, 20]] [[ 1 7 0] [ 6 2 5] [ 7 8 9] [41 10 20]]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Evaluate Einstein's summation convention of two multidimensional NumPy arrays
- Python - Convert Lists to Nested Dictionary
- Python | Check if a nested list is a subset of another nested list
- How to access different rows of a multidimensional NumPy array?
- Program to access different columns of a multidimensional Numpy array
- Python - Convert Lists into Similar key value lists
- Python | Split nested list into two lists
- Python | Merging nested lists
- Python - Nested Records List from Lists
- Python Lists VS Numpy Arrays
- Multidimensional data analysis in Python
- Python | Creating Multidimensional dictionary
- Python | Convert a nested list into a flat list
- Python | Convert list of nested dictionary into Pandas dataframe
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert given list into nested list
- Python | Convert nested sublist into tuples
- Python | Convert string List to Nested Character List
- Python - Convert Flat dictionaries to Nested dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

