Why is it square_list.sort() and not sort(square_list)


As the title says: Why do I have to use square_list.sort() here and why does sort(square_list) not work ? I have not imported a module to use the sort functionality. At this time I dont understand what the real inherent difference is and when to go for sort(object) and when to go for object.sort() ?

start_list = [5, 3, 1, 2, 4]
square_list = []

# Your code here!
for num in start_list:
    square_list.append(num ** 2)

print square_list


the built in function is sorted, and you are allowed to use it:

square_list = sorted(square_list)

sorted is a built in function, where .sorted() is a method of list


Thanks for the quick reply.

I tried using sq = sorted(squared_list) and this works too, however I am only left more confused as I have no idea why you would choose 1 over the other.


use .sort() since it is part of the methods for lists, it is designed for list.


Ah, a method is a function within a class.


yes, and list is a data type class.

this is pushing the limit of my understanding:

when you create a list, a list object is created. (a object is an instance of a class), you can call methods on an object. Which is preferable to a built in function (sorted())


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.