[Lists] vs [[NumPy]] in Python — with example

I always wonder as lists are one of the important data structure of python, as we can perform all the operations with lists and want to know why NumPy arrays are special.

Before moving to differences between them, let’s have a small introduction on both of these.

Photo by Markus Spiske on Unsplash

Lists : List is a collection of elements, which can be written as a list of comma-separated items between square brackets. An important feature about a list is that the items in a list need not be of the same type , each element of a list is accessed using an index value. In python, index of lists starts from 0 and so forth.

Photo by Kelly Sikkema on Unsplash

List is a mutable ( changeable) sequence of data types.

Syntax -

Examples -


NumPy stands for Numerical Python.

NumPy is one of the library in python, which is a table of elements (usually numbers), all of the same type. NumPy’s array class is called ndarray. Here numpy.array is different from array from python library class. An array can only handle one — dimentional array and offers less functionality, where as NumPy has no limitations as arrays.

Importing NumPy package is as follows,

Creating a NumPy array -

One of the way to how to create an NumPy array -

Creating a NumPy array using a tuple -

Now lets see the differences…

Lists Vs NumPy -

In software programming, when looking at differences, the main objectives will be with memory and performance.

1. Operation Wise -

Using NumPy arrays we are able to perform advanced mathematical operations on large data more efficiently than Python Lists.

2. Memory Wise -

NumPy arrays takes less memory than Lists, now lets see this with an example.

Here, I am creating a list using range() function and a NumPy array using arange() function. Lets find the size of the list by multiplying the size of an element with length of list.

Calculating memory occupied by a list -

Output -

Now lets see memory occupied by a NumPy array -

Here we are multiplying an item of a NumPy array with size of the array.

Output -

As we can see there is quite lot of difference between them, so we can say NumPy arrays occupy very less space than lists.

3. NumPy arrays are Faster than lists -

Lets see how Numpy arrays are faster than python lists while performing any operation

Adding two lists and checking the execution time here,

Output for Lists -

Adding two NumPy arrays and checking the execution time here,

Output for NumPy -

Not only about the fastness, we can see how easy to add NumPy arrays than lists as we have to run loop for adding lists.

Hence, we can conclude that NumPy arrays are faster and convenient than python lists.

Software Engineer, Loves Open Source