Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

What is the difference between sort() and sorted() in Python?

T Sowmya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Sorting is the act of rearranging a given sequence. In Python, sorting is easily done with the built-in methods sort () and sorted().

  • The sorted() and sort() methods sort the given sequence in either ascending or descending order.

Though these two methods perform the same task, they are quite different.

sorted()

Syntax

sorted(iterable, key, reverse=False)

This method doesn’t change the original list. (Refer Example 1, line 8)

  • Return Type : returns a sorted list. (Refer Example 1, line 7)

  • Iterable: It can be operated on any sequence (list, tuple, string), collection (dictionary, set, frozenset), or other iterator that needs to be sorted. (Refer Example-2)

sort()

Syntax

List_name.sort(key, reverse=False)

This method makes changes to the original sequence.(Refer example-1,line-13)

  • Return Type: returns none, has no return value.(Refer Example-1,line-12)

  • sort() is a method of the list class, so it can only be used with lists.

Example 1

# 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

Example 2

#Using diff datatypes sorting through sorted() method
courts=["Supreme","High","District"]
print(sorted(courts))#list
courts=("Supreme","High","District")#tuple
print(sorted(courts))
courts="high"#string
print(sorted(courts))
courts={'Supreme':1,'High':2,'District':3}#dictionary
print(sorted(courts))
courts={"Supreme","High","District"}#sets
print(sorted(courts))
#sort() is used only for lists
#print("using sort():",courts.sort())
# attribute error
remove "#' in line 15 before print to check for sort() (anyhow gives error)

RELATED TAGS

python
community creator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring