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?
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!)
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.
.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?
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)”
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.