# [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.

**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.

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

Syntax -

`list_name = [element1,element2,element3.....]`

Examples -

`list_name1 = [1, 2, 3, 4, 5 ]`

list_name2 = ["Abby", "Alexa", "Bob"]

list_name3 = [1,3.5,"Tom"]

**NUMPY -**

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,

`import numpy as np`

**Creating a NumPy array -**

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

`arr = np.array([1, 2, 3])`

Creating a NumPy array using a tuple -

`arr = np.array((1, 2, 3))`

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 -

`l = range(100)`

print(sys.getsizeof(7)*len(l))

Output -

`2800`

Now lets see memory occupied by a NumPy array -

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

`a = np.arange(100)`

print(a.size * a.itemsize)

Output -

`800`

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,

# Adding two python lists

list1 = range(100000)

list2 = range(100000)start = time.time()

list_sum = [(a,b) for a,b in zip(list1,list2)]

end = time.time()

print(f"Adding Time for lists - {end - start}")

Output for Lists -

`Adding Time for lists - 0.08425045013427734`

Adding two NumPy arrays and checking the execution time here,

# Adding two numpy arrays

np1 = np.arange(100000)

np2 = np.arange(100000)start = time.time()

np_sum = np1 + np2

end = time.time()

print(f"Adding Time for NumPy arrays - {end - start}")

Output for NumPy -

`Adding Time for NumPy arrays - 0.0008752346038818359`

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.