'How to solve 'list' object attribute 'sort' is read-only

I am trying to measure euclidean distance between test image and a given dataset. I have multiple values. Now I would like to sort them in ascending order but I am unable to do that. please help to solve this issue. code:

    for j in range(50):

      f = sqrt(sum(j - test) ** 2)

      p = sorted(f, reverse= False)

some of output of f is:

    305753.0

    212825.0

    215385.0

    218201.0

    220761.0

    223833.0

    226905.0


Solution 1:[1]

You need to put all the values in a list and sort it after the loop

values = []
for j in range(50):
    values.append(sqrt(sum(j - test) ** 2))
p = sorted(f)

With list comprehensions

values = [sqrt(sum(j - test) ** 2) for j in range(50)]
p = sorted(f)

Note that reverse=False is redundant.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1