Why is the sorted()
function considered more direct than the .sort()
method used in lists with tuples?
Why is the sorted()
function considered more direct than the .sort()
method used in lists with tuples?
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 ()