Why you should use NumPy arrays instead of nested Python lists?


In this article, we will show you why to use NumPy arrays instead of nested Python lists, and the similarities and differences between them.

Python NumPy Library

NumPy is a Python library designed to work efficiently with arrays in Python. It is fast, simple to learn, and efficient in storage. It also improves the way data is handled for the process. In NumPy, we may generate an n-dimensional array. To use NumPy, we simply import it into our program, and then we can easily use NumPy's functionality in our code.

Python Nested Lists

A Python list is a mutable and ordered collection of elements. In Python, lists are denoted by square brackets.

  • It is possible for the nested lists to be homogenous or heterogeneous.

  • On the list, element-wise operations are not possible.

  • Python lists are one-dimensional by default. However, we can make an N-Dimensional list. But it will also be a 1D list storing another 1D list.

  • A list's elements do not have to be contiguous in memory.

  • Python lists are useful general-purpose containers that can be used for inserting, adding, deleting, and concatenating data.

  • Lists have two drawbacks: they don't enable "vectorized" operations like element-wise addition and multiplication, and because they can contain objects of different types, Python must store type information for each element and execute type dispatching code when working on each element.

NumPy Arrays

  • NumPy is more efficient and convenient since it offers a huge number of vector and matrix operations for free, reducing effort and code complexity. Numpy is also more efficient when compared to nested loops.

  • NumPy arrays are faster and include more built-in functions for doing FFTs, convolutions, rapid searching, linear algebra, basic statistics, histograms, and other tasks.

  • Numpy arrays allow for advanced mathematical and other operations on huge amounts of data.

  • The array is Homogeneous by default, which implies that all data in the array must be of the same Datatype. (Note that a structured array can similarly be created in Python.)

  • Element-by-element operation is feasible.

  • Numpy array includes a variety of functions, methods, and variables to help us with our matrix computation work.

  • An array's elements are kept together in memory. A two-dimensional array, for example, must have the same number of columns in all of its rows. A three-dimensional array, on the other hand, must have the same number of rows and columns on each card.

Representation of Nested Lists

Example

# creating a nested list inputList= [[2, 7, 8], [1, 5, 4]] # printing nested List print(inputList)

Output

On executing, the above program will generate the following output −

[[2, 7, 8], [1, 5, 4]]

Representation of 1-Dimensional Numpy array

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the numpy module with an alias name(np).

  • Use array() function(returns an ndarray. The ndarray is an array object that satisfies the given requirements) of the numPy module to create a 1-Dimensional numpy array.

  • Print the 1-Dimensional array

# importing numpy module with an alias name import numpy as np # creating a 1-dimensional numpy array inputArray = np.array([12, 4, 8, 6]) # printing 1-D array print(inputArray)

Output

On executing, the above program will generate the following output −

[12 4 8 6]

Representation of Multi-Dimensional Numpy array

Example

# importing numpy module with an alias name import numpy as np # creating a multi-dimensional numpy array inputArray = np.array([(2, 7, 8), (1, 5, 4)]) # printing multi-dimensional array print(inputArray)

Output

On executing, the above program will generate the following output −

[[2 7 8]
 [1 5 4]]

Advantages of Using Numpy Arrays Over Python Lists

  • Uses less memory.

  • Faster than the Python List.

  • Simple to use.

Similarities between a list and an array

  • They both make use of square brackets ([]).

  • Both arrays and lists are mutable(elements of both can be modified).

  • Both the NumPy arrays and lists can be indexed and can be used for slicing.

Differences between Numpy array and Python List

Numpy Array Python List
Arrays can directly handle mathematical operations A list cannot do mathematical operations directly.
Consumes less memory than a list Consumes more memory
Array is faster than a list Lists is relatively slower as compared to array
Bit complex to modify Easier to modify
Array cannot include different data sizes A list can include several nested data sizes.
Arrays cannot store different data types list can store different data types

Comparison between Numpy array and Python List

Memory consumption

# importing numpy module with an alias name import numpy as np # importing system module import sys # creating a nested list inputList= [[2, 7, 8], [1, 5, 4]] # printing the size of each item of list(bytes) print("The size of each item of list(bytes) = ",sys.getsizeof(inputList)) # printing size of the entire list(bytes) print("The size of the entire list(bytes) = ",sys.getsizeof(inputList)*len(inputList)) # creating a Numpy array with same elements inputArray= np.array([(2, 7, 8), (1, 5, 4)]) # printing each element of Numpy array(bytes) size print("Each element of Numpy array(bytes) size = ",inputArray.itemsize) # printing the entire Numpy array(bytes) size print("The entire Numpy array(bytes) size = ", inputArray.size*inputArray.itemsize)

Output

On executing, the above program will generate the following output −

The size of each item of list(bytes) = 88
The size of the entire list(bytes) = 176
Each element of Numpy array(bytes) size = 8
The entire Numpy array(bytes) size = 48

We can observe that the nested list consumes significantly more memory than the numpy arrays.

Conclusion

In this article, we learned why NumPy arrays are more useful in Python than nested lists. We used an example to demonstrate the memory efficiency of NumPy arrays over nested lists.

Updated on: 20-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements