0

the method "sort()"

What is the function of sort() ?

2nd Jun 2020, 7:05 PM
Moha Riad
Moha Riad - avatar
2 Respuestas
+ 6
The difference of sort() and sorted() in short words: sort() - using sort() will change (sort) the passed list to the function inline, that means that the original list will be sorted. sort() only works for lists. sorted() - using sorted() will take any iterable (list, tuple, string, dict), not only list, and will return a new sorted iterable. The initial iterable keeps unchanged.
2nd Jun 2020, 7:34 PM
Lothar
Lothar - avatar
+ 1
If you have a list it helps to sort the list. Simple example would be list_1=[2,7,9,4,6,1] list_1.sort() print(list_1) Output will be [1,2,4,6,7,9]..
2nd Jun 2020, 7:09 PM
ardy
ardy - avatar