×

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 program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

Python Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Python Coroutine

Introduction In Python, Coroutines are defined as the special type of function that freely allows control to its caller without losing the state. Coroutines and generates are similar but coroutines consist...

3 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Python Bubble Sort

Bubble sort is one of the techniques used to sort the elements in lists in a certain order, either in ascending or descending order. It is called Bubble sort because...

4 minutes read.