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, 2019 in Python by Arvind
• 3,050 points
1,133 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, 2019 by Neel
• 3,020 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
1,257 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, 2019 in Python by Shashank
• 1,370 points
1,018 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,710 points

reshown Aug 24, 2018 by Priyaj 1,350 views
+7 votes
8 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
126,818 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,434 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
4,071 views
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, 2019 in Python by Arvind
• 3,050 points
681 views
0 votes
1 answer

What is the procedure to find the exact path of python installation in Windows?

This is a very simple thing to ...READ MORE

answered Aug 26, 2019 in Python by Neel
• 3,020 points
747 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