0

I'd like to sort a list with a custom cmp. The original list can be destroyed. But sorted() will return a list. I assume that it involves copying. Each element in the original list is not a small object, I want avoid copying and reuse as much the original list as possible.

Is there something like this available in python?

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 6
    You can use `list.sort`, which sorts in-place. However, the size of the individual elements of the original list won't matter, as Python will just copy references, not contents. – L3viathan Oct 03 '19 at 15:57
  • Possible duplicate of [What is the difference between \`sorted(list)\` vs \`list.sort()\`?](https://stackoverflow.com/questions/22442378/what-is-the-difference-between-sortedlist-vs-list-sort) – Georgy Oct 03 '19 at 16:10
  • The elements aren't copied, only a new list – juanpa.arrivillaga Oct 03 '19 at 17:33
  • @L3viathan Did you mean `sorted()` only copy references but the contents? – user1424739 Oct 03 '19 at 20:38
  • 1
    @user1424739 Yes, that's how assignment works in general, in Python. – L3viathan Oct 04 '19 at 07:56

0 Answers0