 # FAQ: Code Challenge: Lists - Combine Sort 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:

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

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

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

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.

2 Likes

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

Why this doesn’t work???

``````def combine_sort(lst1,lst2)
new_lst = list(zip(lst1, lst2))
new_lst.sort()
return new_lst
``````

First, make sure you put a colon after def combine_sort(lst1, lst2).

Second, the zip function maps elements from the provided iterables together, returning a tuple. So instead of combining the two lists into one new list, as the module requested, you’ve created one new list of multiple tuples, comprised of elements from both original lists:

[(2, 5), (4, -10), (5, 10), (10, 2)]

Notice how your sort function didn’t accurately sort the values, because your list actually has 4 elements instead of the intended 8.

Also, what if the two lists being combined were of varying length? The zip function stops when the shortest input iterable runs out of elements to map against the longer input iterable. Your resulting list would actually leave elements unaccounted for in this case.

What’s the difference between:

new_list = list1 + list2

and

new_list = zip(list1, list2) ?

First big difference, a single linear list vs. a list of tuples.

If we try to sort a list of tuples, it will be by first element in each tuple, and will still return a list of tuples.

``````sorted(lst1 + lst2)
``````

will produce a linear sorted list of both combined.

1 Like

In your first example, `new_list = list1 + list2`, `list2`, `new_list` will be a list containing the elements of `list2` appended to the end of `list1`.
For example:

``````list1 = ['A', 'B', 'C', 'D', 'E']
list2 = [1, 2, 3, 4, 5]

new_list = list1 + list2
print(new_list)
``````

Output:

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, 1, 2, 3, 4, 5]

`zip()` is an entirely different operation.
It will return a `zip object` which is actually an `iterable`. See this post for more on what that means. Basically, you can only iterate through its values once unless you first convert it into a list.
For Example:

``````list1 = ['A', 'B', 'C']
list2 = [1, 2, 3]

new_list = zip(list1, list2)

print('new_list:', new_list) #shows zip object and memory location

#iterate the iterable, new_list, printing its values
for element in new_list:
print(element) #each element is a tuple

print('new_list:', new_list) #the zip object still exists in the same location
#try to iterate and print a second time
for element in new_list:
print('This will not print because new_list is now empty ', element)

#count the number of items in new list
new_list_count = sum(1 for _ in new_list)
print('new_list_count:', new_list_count) # 0 because new_list is indeed empty
``````

Output:

new_list: <zip object at 0x7f985dae7e08>
(‘A’, 1)
(‘B’, 2)
(‘C’, 3)
new_list: <zip object at 0x7f985dae7e08>
new_list_count: 0

If we convert the zip object to a list we can iterate through the list of tuples as many times as we need to:

``````list1 = ['A', 'B', 'C']
list2 = [1, 2, 3]

#zip the two lists and convert the zip object to a list
new_list = list(zip(list1, list2))

print('new_list:', new_list) #prints the contents of new_list

#iterate the list, new_list, printing its values
for element in new_list:
print(element) #each element is a tuple

print('new_list:', new_list) #prints the contents again
#try to iterate and print a second time
for element in new_list:
print(element)

#count the number of items in new_list
#could also simply use len(new_list) instead since
#new_list is now a list
new_list_count = sum(1 for _ in new_list)
print('new_list_count:', new_list_count) # 3 because new_list is not empty
``````

Output:

new_list: [(‘A’, 1), (‘B’, 2), (‘C’, 3)]
(‘A’, 1)
(‘B’, 2)
(‘C’, 3)
new_list: [(‘A’, 1), (‘B’, 2), (‘C’, 3)]
(‘A’, 1)
(‘B’, 2)
(‘C’, 3)
new_list_count: 3

As @mtf stated there is a big difference.

2 Likes