To normalise the values in a list of tuples with list comprehension, the list needs to be transposed, normalised and transposed back. Here is the code to do that.

Normalise list of tuples in Python. No libraries used.

Here is a list of tuples that need to be normalised:
(2, 10),
(3, 15),
(4, 16),
(5, 20),
(6, 45)]
The result should be a normalised list of tuples where the data in each column has a value between 0 and 1:
(0.0, 0.0)
(0.25, 0.14285714285714285)
(0.5, 0.17142857142857143)
(0.75, 0.2857142857142857)
(1.0, 1.0)]
The following function shows how to achieve this with list comprehensions in Python:
def normalise(matrix):
    transposed = [[row[col] for row in matrix] for col, _ in enumerate(matrix[0])]
    normalised = [[(v - min(r)) / (max(r) - min(r)) for v in r] for r in transposed]
    return [tuple([row[col] for row in normalised]) for col, _ in enumerate(normalised[0])]
Full code:
def normalise(matrix):
    transposed = [[row[col] for row in matrix] for col, _ in enumerate(matrix[0])]
    normalised = [[(v - min(r)) / (max(r) - min(r)) for v in r] for r in transposed]
    return [tuple([row[col] for row in normalised]) for col, _ in enumerate(normalised[0])]

X = [(2, 10), (3, 15), (4, 16), (5, 20), (6, 45)]
print(X)
print(normalise(X))
Ouput:
[(2, 10), (3, 15), (4, 16), (5, 20), (6, 45)]
[(0.0, 0.0), (0.25, 0.14285714285714285), (0.5, 0.17142857142857143), (0.75, 0.2857142857142857), (1.0, 1.0)]
Written by Loek van den Ouweland on 2021-10-18.
Questions regarding this artice? You can send them to the address below.