×

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

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

API Requests using Python

What is an API? API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other....

5 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.