FAQ: Working with Lists in Python - Sorting Lists I

This community-built FAQ covers the “Sorting Lists I” exercise from the lesson “Working with Lists in Python”.

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

Computer Science
Data Science

Learn Python 3

FAQs on the exercise Sorting Lists I

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!

1 Like

2 posts were split to a new topic: Why does this code print none?

6 posts were merged into an existing topic: Why does this code print none?

6 posts were split to a new topic: What is the method of order for sorting the addresses?

5 posts were split to a new topic: How can I sort a zipped object?

How do I save the previous order of the list? I tried it with another variable, that one was sorted too though.
### Exercise 4 ###
cities = ['London', 'Paris', 'Rome', 'Los Angeles', 'New York']
unsorted_cities=cities
print(unsorted_cities)
cities.sort() #This returns nothing.
sorted_cities = cities
print(sorted_cities)
print(unsorted_cities) #huh.. this is sorted too.

is it because it is like in C, just a pointer that points to the same address?

Yes, both variable point to the same list in memory.

also, .sort() orders the list in place/memory.

guys why I am getting none as output

Because .sort() sorts the in place. sort updates the existing list.

What is the sorting algorithm used in this sort function? Quick Sort, Bubble Sort, merge sort, something else?.. (wow there are a lot of different sorting functions out there!)

sorted() use timsort:

https://stackoverflow.com/questions/10948920/what-algorithm-does-pythons-sorted-use

So assume .sort() uses something similar?

2 Likes

Yep! Awesome. I had not hear of that one, but looking it up calls out it’s use in python sort() and sorted!

1 Like

When we use .sort() we update the existing list. Is there a way to reverse this update? Or using the sorted() function is the only way to keep the original list?

no, .sort() was deliberate implemented/build to update the existing list. If you don’t want this, you will have to use something else or build your own algorithm/function/implementation.

1 Like

.sort() doesn’t return any value. I don’t why this question asks us to assign it to a variable called “sorted_list”! sort() updates an existing variable. In this exercise we must first update cities (cities.sort(reverse=True) then assign it to the variable “sorted_list”. Otherwise the Python compiler gives us a “none” (null) value.

In this exercise I am supposed to use .sort(). But I don’t understand the sorting of the addresses, Neither the numbers nor the letters seems to be in order. What does this function do here?

1 Like

me too, get None.
Probably as already said because .sort sorts in the variable directly and not when is saved to another one

But if we read carefully the instruction they say to print ‘cities’ and not sorted_cities.

Could anybody explain why I need to add a boolean to sort a list in reverse order? Why do I have to write “.sort(reverse_True)” and not just “.sort(reverse)”

Documentation: https://docs.python.org/3/library/stdtypes.html#list.sort

sort() accepts two arguments that can only be passed by keyword (keyword-only arguments):

key specifies a function of one argument that is used to extract a comparison key from each list element

reverse is a boolean value. If set to True , then the list elements are sorted as if each comparison were reversed.

2 Likes