What is the difference between `sorted(list)` vs `list.sort()`?

后端 未结 6 2214
温柔的废话
温柔的废话 2020-11-22 09:19

list.sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original li

6条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 09:52

    The .sort() function stores the value of new list directly in the list variable; so answer for your third question would be NO. Also if you do this using sorted(list), then you can get it use because it is not stored in the list variable. Also sometimes .sort() method acts as function, or say that it takes arguments in it.

    You have to store the value of sorted(list) in a variable explicitly.

    Also for short data processing the speed will have no difference; but for long lists; you should directly use .sort() method for fast work; but again you will face irreversible actions.

提交回复
热议问题