Python Challenges Lists - Combine Sort

I just finished learning LISTS (chapter) in Python course and there is a challege at the end - 5. Combine Sort
I did this code:

#Write your function here
def combine_sort(lst1, lst2):
  together = sorted(lst1 + lst2)
  return together
#Uncomment the line below when your function is done
print(combine_sort([4, 10, 2, 5], [-10, 2, 5, 10]))

I did a shorter version, that seemed to work, but I wonder, did I do it right and is there any difference with the code provided as a solution by CodeAcademy? Which is:

#Write your function here
def combine_sort(lst1, lst2):
  together = sorted(lst1 + lst2)
  all = sorted(together)
  return all
#Uncomment the line below when your function is done
print(combine_sort([4, 10, 2, 5], [-10, 2, 5, 10]))

Cheers. :v:

Why would you sort, and sort again? Do you have the exercise url/link? I am curious

I would argue that list concatenation happens first (lst1 +lst2), so then sorting one times is enough. And the intermediate variable isn’t used, so you can just do:

 return sorted(lst1 + lst2)
1 Like

I’m guessing the codecadamy together was meant to just concat the two lists like so:

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

This would break the steps down (although variable names are questionable) and make it a bit clearer without know the order of operations.

So together is the first step of joining the lists.
all is the two lists together and now sorted.
And then you return the sorted list.

As @stetim94 stated, you can remove the variables and just return sorted(lst1 + lst2). I’m guessing the example is verbose for the purpose of making it easier to follow and as I said above together probably shouldn’t have had the sort in it.