Python list sort vs sorted

A concise answer from this SO post:

  • sorted returns a new list, list.sort() mutate the calling list and returns None
  • For lists, list.sort() is faster than sorted() because it doesn’t have to create a copy. For any other iterable, you have no choice.
 

Cheng

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami