Skip to content
Advertisement

Use List Comprehension to get the top n items

I tried:

JavaScript

but that won’t work because sort() doesn’t return the sorted list

JavaScript

Advertisement

Answer

.sort() sorts the list in-place

list.sort() sorts the list in-place, mutating the list indices, and returns None (like all in-place operations)

so you’d have to do this:

JavaScript

Output:

JavaScript

The difference between .sort() and sorted() in Python.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement