Sign Up

Continue with Facebook
Continue with Google
or use

Have an account? Sign In Now

Sign In

Continue with Facebook
Continue with Google
or use

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Continue with Facebook
Continue with Google
or use

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Ansfind

Ansfind Logo Ansfind Logo Ansfind Logo Ansfind Logo
Add question

Ansfind Navigation

  • About Us
  • Groups
    • Add group
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Hot Questions
  • Polls
  • Tags
  • Points
  • Users
  • FAQs
  • Help
  • Contact Us
Weboptify
Weboptify
Asked: September 4, 20212021-09-04T14:13:59+05:30 2021-09-04T14:13:59+05:30In: Technology

What is the sort() method in python?

what is the sort() method in python? How to order by Descending or Ascending?

pythonsort method
  • 2
  • 11
  • 24
  • Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
Answer

    Related Questions

    • Does Python have an ordered set?
    • How many keywords present in the python programming language?
    • Which of the following option is not a core data type in the python language?

    1 Answer

    • Voted
    • Oldest
    • Recent
    1. SaRa Professor
      2021-09-04T16:40:20+05:30Added an answer on September 4, 2021 at 4:40 pm

      The sorted() and sort() methods in Python can be used to sort data.
      I’ll show you how to use the sorted() and sort() methods using code examples and explain the differences between them.

      Sort() method in python

      Sort() method accepts a list and arranges it in a specific order. There is no return value for Sort() method.

      Below example, We have a list of numbers that we can sort in ascending order using the sort() method.

      <em>list1 = [34, 21, 1, 345, 47] 
      print("Unordered list: ", list1)</em>
      <em>//this prints the unordered list 
      list1.sort() </em>
      <em>//sorts the list in place 
      print("Ordered list: ", list1)</em>
      <em>//this prints the ordered list1</em>
      Unordered list : [34, 21, 1, 345, 47]
      Ordered list: [1, 21, 34, 47, 345]

      If the list has already been sorted, the console will return None.

      key and reverse are two optional arguments for Sort() method.

      sort() method key has the value it will be called on each item in the list.

      sort() method reverse has a value it will be called on each item in the list here the value is boolean of true or false. if reverse=true means the sorted list will be in reverse alphabetical order.

      Sorted() method in python

      The sorted() method returns a new sorted list from any iterable. Lists, strings, and tuples are examples of iterables.

      Sorted() method also having same two optional arguments key and reverse.

      Below example, A list of numbers has been sorted in ascending order. if we use reverse=True computer will reverse the list from greatest to smallest.

      numbers = sorted([88, 33, 5, -3, 1000], reverse=True)
      print("Sorted in descending order: ", numbers)
      
      Sorted in descending order:[1000, 88, 33, 5, -3]
      
      

       

       

      • 2
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    Leave an answer

    Leave an answer
    Cancel reply

    Browse

    Choose from here the video type.

    Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".

    Sidebar

    Top Users

    Weboptify

    Weboptify

    • 3,268 Points
    Wish Lay

    Wish Lay

    • 2,315 Points
    Manu Murthy

    Manu Murthy

    • 1,602 Points

    Adv 250x250

    • About Us
    • Home
    • Communities
    • Polls
    • Tags
    • Points
    • Users
    • Help
    • Contact Us
    • Referrals
    • Policies
      • Privacy policy
      • Terms of use
    Revenue

    © 2022 Ansfind. | Designed by Weboptify

    monetization_on category home emoji_events
    search
    help

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.