Computer Science
What is the difference between sort( ) and sorted( )?
Python List Manipulation
Answer
sort( ) | sorted( ) |
---|---|
It modifies the list it is called on. That is, the sorted list is stored in the same list; a new list is not created. | It creates a new list containing a sorted version of the list passed to it as argument. It does not modify the list passed as a parameter. |
It works on a list and modifies it. | It can take any iterable sequence type, such as a list or a tuple etc, and it always returns a sorted list irrespective of the type of sequence passed to it. |
It does not return anything (no return value). It modifies the list in place. | It returns a newly created sorted list. It does not change the passed sequence. |
Answered By
Related Questions
What is the difference between appending a list and extending a list?
Do functions max( ), min( ), sum( ) work with all types of lists.
What is the difference between following two expressions, if lst is given as [1, 3, 5]
(i) lst * 3 and lst *= 3
(ii) lst + 3 and lst += [3]Given two lists:
L1 = ["this", 'is', 'a', 'List'], L2 = ["this", ["is", "another"], "List"]
Which of the following expressions will cause an error and why?
- L1 == L2
- L1.upper( )
- L1[3].upper( )
- L2.upper( )
- L2[1].upper( )
- L2[1][1].upper( )