FAQ: Working with Lists in Python - Sorting Lists I


#1

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

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!


#2

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)


#4

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


#5

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?’


#6

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)


#7

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?


#8

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


#9

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?


#10

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