Wrong intruction in the "Working with Lists" in the Fundamentals in Python

Hey everyone and more specifically the creators of the teaching and learning material. I have noticed that there is a mistake in the way one of the instructions is introduced in the “Working with Lists” at the “Fundamentals in Python” course.

More specifically, it is at step 10/12 and within it, the 5th instruction. You are asking to sort in reverse the “cities”, but the expected result is only deemed as correct if you sort the “sorted_cities”.

I thought it might be useful to fix, since it can confuse some learners.

All best,
Charoula

Please include a link to the lesson that you’re referring to.

This worked for me:

cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse = True)
print(cities)
>>['Rome', 'Paris', 'New York', 'Los Angeles', 'London']

1 Like

Here is the link, although it looked like you are at the correct place.
https://www.codecademy.com/journeys/computer-science/paths/cscj-22-intro-to-programming/tracks/cscj-22-fundamentals-of-python/modules/cscj-22-python-lists/lessons/use-python-list/exercises/sort

In the image you can see that if I sort reverse the cities, I get the error. It is only fixed if I sort the sorted.cities as I mentioned above.

Part of your code is obscured by the error message in the screenshot. So, the exact code you wrote is unclear.

Your screenshot shows a list being printed due to print(addresses).
The second output being shown is None.

Do note that the sort method doesn’t return the sorted list, it just sorts the list in place. Have a look at the previous post by lisalisaj.

As the documentation mentions:

This method modifies the sequence in place for economy of space when sorting a large sequence. To remind users that it operates by side effect, it does not return the sorted sequence (use sorted() to explicitly request a new sorted list instance).

I went back and cleared my workspace to see what you were getting at.

Additionally, (and what @mtrtmk said) If you double check the hint for 4, which says, " The .sort() method does not return any value and thus does not need to be assigned to a variable. This is why we will see the value of None as our output for sorted_cities"

Here’s what I wrote:

cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
sorted_cities = cities.sort()
#cities.sort()
#print(cities)
print(sorted_cities)
cities.sort(reverse = True)
print(cities)


>>>
None
['Rome', 'Paris', 'New York', 'Los Angeles', 'London']
1 Like

Let me start over with the code. I added the print(cities) in the code, to show that the response is the same as @lisalisaj.

# Checkpoint 1 & 2
addresses = ["221 B Baker St.", "42 Wallaby Way", "12 Grimmauld Place", "742 Evergreen Terrace", "1600 Pennsylvania Ave", "10 Downing St."]
addresses.sort()
print(addresses)
# Checkpoint 3
names = ["Ron", "Hermione", "Harry", "Albus", "Sirius"]
names.sort()


# Checkpoint 4 & 5
cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)
print(cities)
sorted_cities = cities.sort()
print(sorted_cities)

>>>

Output:
['10 Downing St.', '12 Grimmauld Place', '1600 Pennsylvania Ave', '221 B Baker St.', '42 Wallaby Way', '742 Evergreen Terrace']
['Rome', 'Paris', 'New York', 'Los Angeles', 'London']
None

As you can see, the printed result from the cities is reversed, as it is intended by the description of the 5th instruction
" Edit the .sort() call on cities such that it sorts the cities in reverse order (descending).

Print cities to see the result."

But, i get the error “Value for ['Rome', 'Paris', 'New York', 'Los Angeles', 'London'] did not match the expected value.”.

If i add the sort reverse in the sorted_cities, then it accepted it as correct. See code below:

# Checkpoint 1 & 2
addresses = ["221 B Baker St.", "42 Wallaby Way", "12 Grimmauld Place", "742 Evergreen Terrace", "1600 Pennsylvania Ave", "10 Downing St."]
addresses.sort()
print(addresses)
# Checkpoint 3
names = ["Ron", "Hermione", "Harry", "Albus", "Sirius"]
names.sort()


# Checkpoint 4 & 5
cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)
print(cities)
sorted_cities = cities.sort(reverse=True)
print(sorted_cities)

>>> 

Output:
['10 Downing St.', '12 Grimmauld Place', '1600 Pennsylvania Ave', '221 B Baker St.', '42 Wallaby Way', '742 Evergreen Terrace']
['Rome', 'Paris', 'New York', 'Los Angeles', 'London']
None

I hope it is more clear now.

list.sort() sorts a list in place. There is no value returned. which is why it returns “None.”

.sorted() returns a new sorted list from an iterable.

See:

You’re not getting a result here:

cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)
print(cities)
sorted_cities = cities.sort(reverse=True)
print(sorted_cities)
>>['Rome', 'Paris', 'New York', 'Los Angeles', 'London']
None

because you’re trying to print a list that doesn’t return anything. ie: you’ve assigned a variable –sorted_cities to a value that doesn’t return anything…which will result in None

When you do this:

cities.sort(reverse=True)
print(cities)

you get the desired output which is the reversed list. The instructions aren’t wrong here. (possibly confusing tho).

Try commenting out bits of code to see the varying results.

Like:

cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)
print(cities)
#sorted_cities = cities.sort(reverse=True)
#print(sorted_cities)
>>>>>['Rome', 'Paris', 'New York', 'Los Angeles', 'London']

VS.

# Checkpoint 4 & 5
cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
#cities.sort(reverse=True)
#print(cities)
sorted_cities = cities.sort(reverse=True)
print(sorted_cities)
>>None

An aside, sorted() also has the reverse=True parameter:

sorted(cities, reverse=True)
>>>['Rome', 'Paris', 'New York', 'Los Angeles', 'London']

See:
https://docs.python.org/3/howto/sorting.html#ascending-and-descending

1 Like

As lisalisaj has detailed with examples, the .sort() method sorts the list in place but doesn’t return the sorted list. Whereas, the sorted function creates a new list and returns that list.

As for why one version of your code is rejected and the second version is accepted, let me copy paste both versions from your previous post:

Version A (rejected by automated grader)

cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)  # cities is sorted in place in reverse order
print(cities)              # The reversed cities list is printed

# You once again sort cities but in ascending order (default).
sorted_cities = cities.sort()  # None is assigned to sorted_cities

print(sorted_cities)      # None is printed

Version B (accepted by automated grader)

cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)  # cities is sorted in place in reverse order
print(cities)              # The reversed cities list is printed

# You once again sort cities but in reverse order. 
# No change as list already in reverse order.
sorted_cities = cities.sort(reverse=True)  # None is assigned to sorted_cities

print(sorted_cities)      # None is printed

In Version A, you sort cities in reverse order, and then a couple of lines later you sort cities in ascending (default) order undoing your previous sort. Therefore, you get the error message that cities doesn’t match the expected value. Try adding a print(cities) statement in Version A at the very bottom (immediately after the print(sorted_cities) statement). You will see that cities is no longer in reverse order.

In Version B, you sort cities in reverse order, then a couple of lines later you sort cities in reverse order again. But, the second sort is redundant and doesn’t change the already reversed list. So, cities matches the expected value and is accepted by the exercise.

Thank you both very much for the detailed explanations and the patience. It took me a while to wrap my head around it.

Wish you both a good day!

2 Likes