What is difference between sort and sorted in Python?
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
sorted() and sort() methods sort the given sequence in either ascending or descending order.
Sorted(): This method doesn’t change the original list.
Sort(): This method makes changes to the original sequence.
# based on alphabetical order of first letter
courts=[“Supreme”,”High”,”District”]
print(” the original list :”,courts)
# using sorted()
new_list=sorted(courts)
print(“using_sorted():”,new_list)#returns a sorted list
print(“aft_sorting_lst”,courts)#doesn’t change original list
#using sort()
k=courts.sort()
print(“using_sort():”,k)#returns Nothing
print(“aft_sort_lst:”,courts) #changes the original list