Python sorted() Function

Python Programming Training Certification


Flexible Hours

100 Assignments

Instructor Led online Training

50 LMS Access

24X7 Support

100% Skill Level

Enquire Now

4.9 out of 1000+ Ratings
Best Python Institute for Learning Python Course & Training, Live Project Training in Python with Django, Data Science and AI, Interview Assistance, Expert Coaching Trainers. Python Certification & Interview Assistance! Get free demo now!

Course Overview

Python is one of the world’s top programming languages used today and Python training has become the most popular training across individuals. Training Basket’s Python Training & Certification course covers basic and advanced Python concepts and how to apply them in real-world applications.Python is a flexible and powerful open-source language that is easy to learn and consists of powerful libraries for data analysis and manipulation. Our Python training course content is curated by experts as per the standard Industry curriculum. The curriculum, coding challenges and real-life problems cover data operations in Python, strings, conditional statements, error handling, shell scripting, web scraping and the commonly used Python web framework Django. Take this Python training and certification course and become job-ready now.

Python sorted() Function

Python sorted() function is used to sort elements. By default, it sorts elements in ascending order but can be sorted descending also. It takes four arguments and returns collection in sorted order. In the case of a dictionary, it sorts only keys, not values.
The signature of the function is given below.

Signature

  • sorted (iterable[, cmp[, key[, reverse]]])

Parameters

iterable: A collection on which sorting performs.

cmp: A custom comparison function. The default value is None.

key: A function.

reverse: To get sorted collection in reverse order.

Return

It returns a unique integer number.

Let’s see some examples of id() function to understand it’s functionality.

Python sorted() Function Example 1

Here, in this example, we are sorting a string object to understand the function.

  • # Python sorted() function example
    str = “javatpoint” # declaring string
    # Calling function
    sorted1 = sorted(str) # sorting string
    # Displaying result
    print(sorted1)

Output:

  • [‘a’, ‘a’, ‘i’, ‘j’, ‘n’, ‘o’, ‘p’, ‘t’, ‘t’, ‘v’]

Python sorted() Function Example 2

We can use this function to sort any iterable like list, tuple and dictionary. See the example below.

  • # Python sorted() function example
    li = [2003,56,98,659,622,1002,3652]
    tupl = (232,2500,3698,5264,2578,21)
    dic = {3: ‘Three’,4:’Four’,1:’One’,2:’Two’}
    # Calling function
    lisorted = sorted(li) # sorting list
    tupsorted = sorted(tupl) # tuple
    dicsorted = sorted(dic) # dictionary
    # Displaying result
    print(lisorted)
    print(tupsorted)
    print(dicsorted)

Output:

  • [56, 98, 622, 659, 1002, 2003, 3652]
    [21, 232, 2500, 2578, 3698, 5264]
    [1, 2, 3, 4]

Python sorted() Function Example 3

To sort the list into reverse order (descending), pass True in reverse, and we will get the list sorted in reverse order.

  • # Python sorted() function example
    li = [2003,56,98,659,622,1002,3652]
    # Calling function
    lisorted = sorted(li, reverse = True) # Sorting list in descending order
    # Displaying result
    print(lisorted)

Output:

  • [3652, 2003, 1002, 659, 622, 98, 56]

Python sorted() Function Example 4

Here, we are sorting a list by passing a lambda function in the key during the call.

  • # Python sorted() function example
    li = [(2,15),(3,5),(65,5),(8,5)]
    # Calling function
    lisorted = sorted(li, key=lambda x: sum(x)) # Sorting list by getting sum of tuples
    # Displaying result
    print(lisorted)

Output:

  • [(3, 5), (8, 5), (2, 15), (65, 5)]

Recently Trained Students

Jessica Biel

– Infosys

My instructor had sound Knowledge and used to puts a lot of effort that made the course as simple and easy as possible. I was aiming for with the help of the ZebLearn Online training imparted to me by this organization.

Richard Harris

– ITC

I got my training from Zeblearn in the Python Certification Training, I would like to say that say he is one of the best trainers. He has not even trained me but also motivated me to explore more and the way he executed the project, in the end, was mind-blowing.


Candidate’s Journey During Our Training Program

Card image cap

Expert’s Advice & Selection of Module

Choosing the right type of module for the training is half the battle & Our Team of experts will help & guide you.

Card image cap

Get Trained

Get Trained & Learn End to End Implementation from our Expert Trainer who are working on the same domain.

Card image cap

Work on Projects

We Do make our student’s work on multiple case studies , scenario based tasks & projects in order to provide real-time exposure to them.

Card image cap

Placements

We have a dedicated placement cell in order to provide placement assistance & relevant interviews to our candididates till selection

Placement Partner

×

Leave your details

×

Download Course Content