What are the arguments of sorted() function in Python?

0 votes
I want to know the arguments that a sorted function can hold in Python? Any example would really help
Jul 29 in Python by Arvind
• 2,960 points
14 views

1 answer to this question.

0 votes

Sorted() sorts any sequence (list, tuple) and always returns a list with the elements in sorted manner, without modifying the original sequence.

Syntax : sorted(iterable, key, reverse)

Parameters : sorted takes three parameters from which two are optional.

  • Iterable : sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
  • Key(optional) : A function that would server as a key or a basis of sort comparison.
  • Reverse(optional) : If set true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.
x = [2, 8, 1, 4, 6, 3, 7] 
  
print "Sorted List returned :", 
print sorted(x) 
  
print "\nReverse sort :", 
print sorted(x, reverse = True) 
  
print "\nOriginal list not modified :", 
print x 

Output :
Sorted List returned : [1, 2, 3, 4, 6, 7, 8]

Reverse sort : [8, 7, 6, 4, 3, 2, 1]

Original list not modified : [2, 8, 1, 4, 6, 3, 7]
answered Jul 29 by Neel
• 3,010 points

Related Questions In Python

0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
106 views
0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14 in Python by Shashank
• 1,350 points
55 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,670 points

reshown Aug 24, 2018 by Priyaj 71 views
+2 votes
6 answers

What exactly is the function of random.seed() in python?

The seed method is used to initialize the ...READ MORE

answered Oct 29, 2018 in Python by Rahul
25,165 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 281 views
+4 votes
6 answers
0 votes
0 answers

What are the various ways to manipulate a string in Python?

I have a string in Python. I ...READ MORE

Aug 13 in Python by Arvind
• 2,960 points
27 views
0 votes
1 answer