Should I use `.sort()` or `sorted()`?

so i know the solution provided is to use:

def combine_sort(lst1, lst2):
unsorted = lst1 + lst2
sortedList = sorted(unsorted)
return sortedList

but would using .sort() be better? When should I used .sort() instead of sorted()?
i used .sort() first before i checked the solution

def combine_sort(lst1, lst2):
new_list = (lst1 + lst2)
return new_list

Well, what do they do differently, and which of those behaviours do you prefer?
It’s not so much a matter of which you should use, but rather finding out what they do and picking one that suits you.

Not sure what the point of the function/exercise is though… the caller can just as well concatenate and sort themselves

I also did .sort function, my coding looked like this :

def combine_sort(lst1, lst2):
combine = lst1 + lst2
return combine.sort()

When I printed this with .sort(), it printed “None”, while sorted(combine) worked fine. Is there any reason behind why .sort() would have not worked?

1 Like

You cannot return .sort() because the .sort() method rearranges the list internally and doesn’t return anything. Run it after you combine the lists and then just print your combine list.


Only the sorted() function returns a new list. The sort() function rearranges the existing list. Using sorted we have access to the original list in the original order.


This question may sound stupid, but the .sort() function will not work in this case since it is a void function and you cannot return it . Am I right?

systemslayer pretty much covers this issue:

after sorting the list you can return it:

return lst

Got it.Thank you so much

Why does returning lst3.sort() gives an empty list?

When I first sort and then return lst3, it prints the correct result. But when I return lst3.sort() instead, it prints None on the console. Why is that?

Because it is not the list that gets returned, but the return value of the sort method, which is None.

Compare this behavior to,

return sorted(my_list)

What would be the case if I want to use .sort() instead of sorted? I tried .sort() but the result was “None”. Can anyone explain?

lst.sort() is a list method that sorts a list. After you run it, the original list is sorted, period. It is a function (method, actually) that “does something”, rather than one which “returns something.”

Some people, mostly those trained in languages where you are required to state these things explicitly, call the former “void functions”, and the latter, “fruitful functions”.

sorted(), on the other hand, is a Python built-in function which will take an iterable sequence and return a second sequence, namely a sorted version of the first. Fruitful.


Tried to save a line by returning the list with sorted rather than assigning it a separate line.

def combine_sort(lst1, lst2):
  newlist = lst1 + lst2
  return sorted(newlist)

sorted will take any expression that yields a list.

return sorted(lst1 + lst2)
1 Like