0

What is difference between .sort() and .sorted() in python?

22nd Mar 2020, 7:53 AM
Agustín Variable
Agustín Variable - avatar
5 Antworten
+ 3
The sort() is used only on lists and it modifies the original list while the sorted() can be used for both strings and lists, instead of modifying the original list it returns a new list
22nd Mar 2020, 8:05 AM
Justus
Justus - avatar
+ 4
Just to complete the statement from Justus: sorted() is not limited to list and string, it does also work with tuple and with set as argument.
22nd Mar 2020, 9:54 AM
Lothar
Lothar - avatar
+ 2
Check this a =list('4568239') print(sorted(a)) print(a) a.sort() print(a)
22nd Mar 2020, 8:10 AM
Justus
Justus - avatar
0
Works fine. Which?
22nd Mar 2020, 8:15 AM
Justus
Justus - avatar