×

Python sorted() function

Python sorted() function

The sorted() function returns a sorted list of the specified iterable object.

Syntax

sorted(iterable, *, key=None, reverse=False)

Parameter

iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc.

key: It is an optional parameter that signifies a Function to execute to decide the order. 

reverse: It represents a Boolean parameter where the ‘False’ will sort in the ascending order, whereas the ‘True’ will sort in the descending order. 

Return

This function returns a new sorted list from the items in iterable.

Example 1

 # Python Program explaining
 # the sorted() function
 #passing the string values
 str_bal = ("u", "b", "a", "s", "f", "m", "h", "e")
 print("Initial string value:",str_bal)
 #sorting the string values
 sorted_str = sorted(str_bal)
 # priting the sorted string
 print("Sorted String: ",sorted_str) 

Output

Initial string value: ('u', 'b', 'a', 's', 'f', 'm', 'h', 'e')
Sorted String:  ['a', 'b', 'e', 'f', 'h', 'm', 's', 'u']

Example 2

 # Python Program explaining
 # the sorted() function
 #passing the integer values
 int_val = (78,12,45,87,12,34)
 print("Initial integer value:",int_val)
 #sorting the integer values
 sorted_str = sorted(int_val)
 # priting the sorted integer values
 print("Sorted integer: ",sorted_str) 

Output

Initial integer value: (78, 12, 45, 87, 12, 34)
Sorted integer:  [12, 12, 34, 45, 78, 87]

Example 3

 # Python Program explaining
 # the sorted() function
 #passing the values
 val = ("Hello","Apple")
 print("Initial  value:",val)
 #sorting the values
 sorted_str = sorted(val)
 # priting the sorted values
 print("Sorted value: ",sorted_str) 

Output

Initial  value: ('Hello', 'Apple')
Sorted value:  ['Apple', 'Hello']

Related Topics

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

How to create a login page in python

Users can access an application by providing their username and Password or by authenticating with a social media login on the login screen. Python Tkinter Python provides a variety of choices for...

3 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Flutter Python

The flutter is a frame work available in the python programming language, to create web applications, mobile apps. The flutter is generally used for the development of the backend of...

3 minutes read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

Find Words in String Python

Python : Python programming language is considered a general purpose; it is a high-level programming language that is not much difficult but easier to learn. Python programming language is rich in...

5 minutes read.

Python Kwargs Example

In this article, we'll talk about Python's kwargs notion. In Python, kwargs has two stars and passes a variable number of keyworded argument lists to the function, whereas args has...

5 minutes read.

GUI Calendar using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Examples

In this tutorial, we will see some examples related to python. This will include some basic example questions and their code in Python. Write a program to print “Hello Python”. print(‘Hello Python’) Output Hello...

5 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Curdir Python

Python Programming Language Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.