Difference between sort and sorted

0 votes
What is the difference between sort() and sorted() in python? I have seen few code examples and outputs but I don't know the logic. Please explain.
Feb 7, 2019 in Python by Disha
4,342 views

2 answers to this question.

0 votes

sort() basically works with the list itself. It modifies the original list in place. The return value is None.

sorted() works on any iterable that may include list, dictionary and so on. It returns another list and doesn't modify the original list.

answered Feb 7, 2019 by Omkar
• 69,210 points
0 votes

when to use which?

  • Use list.sort when you do not wish to retain the original sort order (Thus you will be able to reuse the list in-place in memory.) and when you are the sole owner of the list (if the list is shared by other code and you mutate it, you could introduce bugs where that list is used.)
  • Use sorted when you want to retain the original sort order or when you wish to create a new list that only your local code owns.

A large sized list being sorted with sorted making a copy will likely dominate differences, but the sorting itself dominates the operation, and organizing your code around these differences would be premature optimization. I would use sorted when I need a new sorted list of the data, and I would use list.sort when I need to sort a list in-place, and let that determine my usage.

answered Mar 26, 2020 by rahul
• 360 points

Related Questions In Python

0 votes
1 answer

What's the difference in Qt between setVisible, setShown and show/hide

show() is just a convenience function for ...READ MORE

answered Apr 17, 2018 in Python by anonymous
7,446 views
0 votes
1 answer

What is the difference between list and tuple?

Lists are mutable(values can be changed) whereas ...READ MORE

answered May 5, 2018 in Python by aayushi
• 750 points
6,520 views
0 votes
1 answer

Difference between '==' and 'is'

'==' checks for the equality of the ...READ MORE

answered May 14, 2018 in Python by Nietzsche's daemon
• 4,260 points
570 views
0 votes
1 answer

Difference between pop and delete methods

I think you mean del() and not ...READ MORE

answered Jun 25, 2018 in Python by Hamartia's Mask
• 1,580 points
1,824 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,058 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,477 views
+6 votes
4 answers

Python and Pygame: Difference

Python is a programming language. PyGame is a ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 58,090 points
4,147 views
+1 vote
2 answers

Measuring the distance between pixels on OpenCv with Python

You can try this: Mat pts1(nPts, 1, CV_8UC2), ...READ MORE

answered Aug 24, 2018 in Python by Omkar
• 69,210 points
10,149 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP