What is the difference between sorted () and .sort ()?

5

Why is the sorted() function considered more direct than the .sort() method used in lists with tuples?

    
asked by anonymous 29.04.2016 / 19:32

1 answer

4

In sort you change the list itself, in sorted you have a value that you can use in a new variable.

a = [2,1,3];
a.sort();
b = a; //a e b possuem o valor [1,2,3]
a = [2,1,3];
b = sorted(a); //a possui o valor [2,1,3] e b possui o valor [1,2,3]
So I do not think one is more straightforward than the other, it simply depends on how you're going to apply the sort, if you need to keep the value cluttered somewhere, use sorted, if the old value does not interest you any more, use .sort ()

    
29.04.2016 / 20:44