Why does a sorted list print as none?

Why does this return none.

Please explain more in detail why things print what they do. I feel some of the explanations are very light hearted.

Sorting Lists I

Exercise 4

cities = [‘London’, ‘Paris’, ‘Rome’, ‘Los Angeles’, ‘New York’]

sorted_cities = cities.sort()
print(sorted_cities)

if you need more explanation, you can always check the documentation:

https://docs.python.org/2/tutorial/datastructures.html

`.sort()` modifies the original list and returns None

4 Likes

If we wanted to save a sorted list as a variable, how would we go about that if sorted_list = list.sort() then print(sorted_list) returns ‘None?’

because `.sort()` modifies the original list, so you can just do:

``````my_list = [6, 5, 4]
my_list.sort()
print(my_list)
``````

otherwise, use the `sorted()` function, which returns a new sorted list (so then you have two lists)

21 Likes

So I was curious about this too. I reckoned that I could make a copy of the original list before sorting it:

Exercise 4

cities = [‘London’, ‘Paris’, ‘Rome’, ‘Los Angeles’, ‘New York’]
unsorted_cities = cities
cities.sort()
print (cities)
print(unsorted_cities)

But that yielded:
[‘London’, ‘Los Angeles’, ‘New York’, ‘Paris’, ‘Rome’]
[‘London’, ‘Los Angeles’, ‘New York’, ‘Paris’, ‘Rome’]

So I moved the print instruction:
cities = [‘London’, ‘Paris’, ‘Rome’, ‘Los Angeles’, ‘New York’]
unsorted_cities = cities
print(unsorted_cities)
cities.sort()
print (cities)

and this produced:
[‘London’, ‘Paris’, ‘Rome’, ‘Los Angeles’, ‘New York’]
[‘London’, ‘Los Angeles’, ‘New York’, ‘Paris’, ‘Rome’]

But I still wondered how to get sorted to print before unsorted, because it looks as though Python is updating unsorted_cities even though it has passed it in the code (is that actually what happens?!).

So I decided to try ensuring that unsorted_cities was actually a list:

cities = [‘London’, ‘Paris’, ‘Rome’, ‘Los Angeles’, ‘New York’]
unsorted_cities = list(cities)
print(unsorted_cities)
cities.sort()
print (cities)
print(unsorted_cities)

which worked, and returned
[‘London’, ‘Paris’, ‘Rome’, ‘Los Angeles’, ‘New York’]
[‘London’, ‘Los Angeles’, ‘New York’, ‘Paris’, ‘Rome’]
[‘London’, ‘Paris’, ‘Rome’, ‘Los Angeles’, ‘New York’]

But I don’t know why. Does anyone?

4 Likes

you have two variables pointing to the same list in memory. Using `list()` will actually ensure a copy of the list

4 Likes

In general how do I know whether functions will modify my original object or just return the result of it’s operation.

What are each of these types of functions called?
a) sorted(object)
b) object.sort()

By that logic, why doesn’t object.count(‘x’) also modify the object?

the course only explains by saying one comes before and other after… What do I need to search for when studying?

if you have written the function yourself, you will know. If its an existing function or method, you will have to check the documentation

why would count need to modify anything?

the problem is that understanding the differences between methods and functions, you need to understand classes, which will come later

If you want to save your sorted list to a new variable.
My advice is to first sort the list, and later make an assignment.

``````cities = ['London', 'Paris', 'Rome', 'Los Angeles', 'New York']
cities.sort()
sorted_cities = cities
print(sorted_cities)
``````
7 Likes

We can also call upon the built-in function, `sorted()` which makes a copy of the list that is passed in to it.

``````sorted_cities = sorted(cities)
``````

This does not alter (mutate) the list given as the argument. It copies the list, then sorts the copy which can then be assigned.

12 Likes

I agree that your method does solve the problem that I was having.

It is a bit ridiulous however that you can use the count function after the period but you can’t use another function. If the sort would be allowed then amazing things could happen.

Now, I got it! Thank you.

Thanks, I got frustrated at first when doing the exercise just because the output is `None`.