###### tags: `Python`,`list`,`Numpy`
# Python List vs. NumPy
References:
<https://numpy.org/doc/stable/user/absolute_beginners.html>
**What’s the difference between a Python list and a NumPy array?**
NumPy gives you an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them. While a Python list can contain different data types within a single list, all of the elements in a NumPy array should be homogeneous. The mathematical operations that are meant to be performed on arrays would be extremely inefficient if the arrays weren’t homogeneous.
Why use NumPy?
NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.
**Sample 1**
```python=
list=[1,"A"]
print(list) #[1, 'A']
#Enhanced List
import numpy as np
arr = np.array([1,"A"])
print(arr,type) #['1' 'A'] <class 'type'>type changed
#all of the elements in a NumPy array should be homogeneous
```
**Sample 2**
```python=
l=list(range(1,5,1))
# l=list(range(1,5,.1)) #list TypeError(only for int)
print(l)#[1, 2, 3, 4]
import numpy as np
#Array Range,numpy (float type is ok)
arr = np.arange(start=1,stop=5,step=.5)
print(arr) #[1. 1.5 2. 2.5 3. 3.5 4. 4.5]
```
**Sample 3**
```python=
l1 = [1,2]
l2 = [2,3]
l3 = l1+l2
print(l3) #[1, 2, 2, 3]
import numpy as np
v1 = np.array([1,2])
v2 = np.array([2,3])
v3 = v1 + v2
print(v3) #[3 5]
```
**Sample 4**
```python=
weight = [96,87,110]
height = [1.74,1.89,1.85]
# BMI
for w, h in zip(weight,height): #zip(generate a list of coordinates)
print(w/h**2)
#31.708283789139912
#24.355421180818006
#32.14024835646457
import numpy as np
weight = np.array ([96,87,110])
height = np.array ([1.74,1.89,1.85])
bmi = weight/height**2
print(bmi) #[31.70828379 24.35542118 32.14024836]
```
**Sample 5**
```python=
l = list(range(1,11))
print(l) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#List Comprehension
l = [x**2 for x in l]
print(l) #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
import numpy as np
arr = np.arange(1,11)
print(arr) #[ 1 2 3 4 5 6 7 8 9 10]
#Vectorizetion
arr = arr **2
print(arr) #[ 1 4 9 16 25 36 49 64 81 100]
```
**Sample 6**
```python=
import time
start = time.time()
l = range(100_0000)#range(0, 1000000)
l = [x*2 for x in l]
print('list=',time.time()-start) #list= 0.1948225498199463
import numpy as np
start = time.time()
l = np.arange(100_0000)
l = l*2
print('np=',time.time()-start) #np= 0.025931835174560547
```
:100: **NumPy arrays are faster**