Software Engineering

# How to Normalize a List of Numbers in Python

If you need to normalize a list of numbers in Python, then you can do the following:

## Option 1 – Using `Native Python`#

``````list = [6,1,0,2,7,3,8,1,5]
print('Original List:',list)
xmin = min(list)
xmax=max(list)
for i, x in enumerate(list):
list[i] = (x-xmin) / (xmax-xmin)
print('Normalized List:',list)
``````

## Option 2 – Using `MinMaxScaler` from `sklearn`#

``````import numpy as np
from sklearn import preprocessing
list = np.array([6,1,0,2,7,3,8,1,5]).reshape(-1,1)
print('Original List:',list)
scaler = preprocessing.MinMaxScaler()
normalizedlist=scaler.fit_transform(list)
print('Normalized List:',normalizedlist)
``````

You can also specify the `range` of the `MinMaxScaler()`.

``````import numpy as np
from sklearn import preprocessing
list = np.array([6,1,0,2,7,3,8,1,5]).reshape(-1,1)
print('Original List:',list)
scaler = preprocessing.MinMaxScaler(feature_range=(0, 3))
normalizedlist=scaler.fit_transform(list)
print('Normalized List:',normalizedlist)
``````