×

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 Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

Python AIOHTTP

Python 3.5 introduced some new syntax that makes it simpler for developers to make asynchronous programmes and packages. Aiohttp, an HTTP client/server for asyncio, is one such package. In essence,...

3 minutes read.

Python Algorithms

A quote goes, "A goal without a plan is just a wish." To achieve anything in life, we can't simply go for it without making a plan and sticking to...

4 minutes read.

Compound Interest GUI Calculator 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...

6 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

What is Sleeping Time in Python

Did you ever postpone a Python program's execution? Usually, you want your code to execute as quickly as possible. But there are times when it is in your best interests...

3 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.