Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNathaniel Camorlinga
798 PointsList sort method returning None
Below is the description of the sort method:
Sort the list in ascending order and return None.
|
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
| order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,
| ascending or descending, according to their function values.
|
| The reverse flag can be set to sort in descending order.
Here is my code:
grades = ["B", "B", "F", "C", "B", "A", "A", "D", "C", "D", "A", "A", "B"]'
grades.sort()`
None
I understand why the method is returning None. While it says the method returns None, I don't think it is being honest. It tells me it takes an object and sorts it in ascending order. If the function did that, where is the new sorted expression stored?
It also does not say anywhere that I have to pass in arguments to this method, so that is also something that confuses me greatly.
Thanks in advance to whoever answers my question.
1 Answer
Chris Freeman
Treehouse Moderator 68,454 PointsHey Nathaniel Camorlinga, it is sometimes difficult to remember which methods return an object and which ones return None
Hereβs a good doc page on sorting that states Python lists have a built-in list.sort()
method that modifies the list in-place.
Where in-place means the list self modifies itself to be sorted.
To leaved a list unchanged use sorted which returns a new list.
new_list = sorted(org_list)
Post back if you need more help. Good luck!!!