FAQ: Code Challenge: Lists - Combine Sort

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Combine Sort” exercise from the lesson “Code Challenge: Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Combine Sort

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


This is the solution and I am still receiving syntax errors. You have to change 1st, 1st1, or 1st2 to first, first1, or first2 to get it to work. The instructions need to be changed or the exercise needs to be fixed.

I think your first function parameter is typed “1st1” instead of “lst1”. Easy mistake to make.

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)
new_list.sort()
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

new_list = lst1 + lst2

instead of writing the code as above under the definition, is it also possible to use .append on both like these?

new_list.append(lst1, lst2)

if not, what’s the reason?

That works so long as both objects are lists. The plus operator (+) is a concatenation tool for combining lists into one.

Have you tried this? What did you get? Let me guess, an exception was raised since append can only take one argument at a time?

If we append a list to a list, we get a list in a list.

>>> s = []
>>> s.append([])
>>> s
[[]]
>>> 

Conclusion? Concatenating two lists is the simplest approach.

return sorted(lst1 + lst2)
1 Like

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.

1 Like

Hi Guys,

Does anybody know why this code doesn’t work?

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

I get a ‘failed to test your code’ message from the console. I’m starting to think that it could also be something to do with the console having issues loading the environment as no ‘Error’ is returned.

Thanks!

Never mind it worked. It was an issue with the environment.

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