| by Arround The Web | No comments

## Python Sorted Reverse

Python is a versatile and popular programming language that offers a range of built-in functions to make programming tasks easier. One such function is the “sorted()” function, which allows you to sort various types of data in either “ascending” or “descending” order which assists especially in mathematical computations.

This article will explain the working of Python’s “sorted()” function to reverse the sorted data.

## What is the “sorted()” Function in Python?

The “sorted()” function in Python is utilized to sort various types of data, including strings, lists, and tuples, in either ascending or descending order. The function accepts an iterable as a parameter/argument and retrieves a new sorted list.

Syntax

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

Parameters

The “sorted()” function takes the following parameters:

• iterable”: This parameter specifies the iterable that we want to sort, such as a string, list, or tuple.
• key”: This is an optional argument that determines a function to be called/accessed on each item of the iterable. The function is used to extract a comparison key from each element, which is then used to sort the iterable.
• reverse”: This optional parameter specifies whether the iterable should be sorted in reverse order. Normally, it is set to “False”, which means that the input iterable is sorted in ascending order.

Return Value

The “sorted()” function retrieves a unique list with the iterable’s elements in sorted order.

Example 1: Applying the “sorted()” Function to Sort String, List, and Tuple

Let’s take a look at the below code using the “sorted()” function to sort the discussed types:

string_value = "python"
sorted_string = sorted(string_value)
print(sorted_string)

list_value = [99, 45, 22, 35]
sorted_list = sorted(list_value)
print(sorted_list)

tuple_value = (96, 12, 33, 13)
sorted_tuple = sorted(tuple_value)
print(sorted_tuple)

In the above code, the created string, list, and tuple are sorted using the applied “sorted()” function in each case, accordingly.

Output

From the above snippet, it can be seen that the elements of the “string”, “list”, and “tuple” are sorted in ascending order.

Example 2: Applying the “sorted()” Function to Sort the Input List in Ascending Order

The “sorted()” function can also be utilized to explicitly sort elements of an iterable in ascending order, as follows:

list_value = [99, 45, 22, 35]
sorted_list = sorted(list_value, reverse=False)
print('Given List: ', list_value)
print('Sorted List: ', sorted_list)

In the above example, explicitly set the “reverse” parameter of the applied “sorted()” function to “False” to sort the created list in ascending order.

Output

Based on the above output, the ascending list has been produced.

Example 3: Applying the “sorted()” Function to Sort the List in Descending Order

The below-provided code is used to sort the provided list in descending order:

list_value = [29, 45, 12, 35]
sorted_list = sorted(list_value, reverse=True)
print('Given List: ', list_value)
print('Sorted List: ', sorted_list)

In the above code lines, the “reverse” parameter is assigned the value “True” to sort the list in descending order instead. As a result, the “sorted()” function returns a unique list with the elements/items in reverse order.

Output

The above output verified that the given list elements have been sorted reversely.

Example 4: Applying the “sorted()” Function With the “Key” Parameter to Sort the List of String

The “key” parameter of the “sorted()” function can also be used to modify the way items in a list are sorted. The function takes the “key” parameter and returns a value that is used to determine the order of the items:

string_value = ["Joseph", "Anna", "Lily", "Molly"]
sorted_str = sorted(string_value, key=len)
print('Given List of String: ', string_value)
print('Sorted List of String: ', sorted_str)

In this example, the “sorted()” function allocates “len” to the “key” parameter to sort the given list of strings based on the length of the alphabet. The “sorted()” function then sorts the list in ascending order based on the length of each word.

Output

The given list of strings has been sorted using the “sorted()” function.

Example 5: Applying the “sorted()” Function With Multiple “Key” Parameters to Sort the List of Dictionary

We can also use multiple keys to sort a given iterable such as a dict, tuple, or list. In this case, the dictionary elements/items are sorted according to the first key “name”, and then the second key “age”. Here is an example code:

dict_value = [{'name': 'Mary', 'age': 23}, {'name': 'Anna', 'age': 22}, {'name': 'Alex', 'age': 19}]
sorted_dict = sorted(dict_value, key=lambda dict: (dict['name'], dict['age']))
print(sorted_dict)

Based on the above code:

• The “sorted()” function sorts the list of dictionaries based on the “name” and “age” keys in the given dictionary.
• The “sorted()” function takes two arguments including the dictionary name “dict_value” and a “key” parameter allocated as “lambda”, respectively.
• The lambda function is utilized as the “key” parameter to sort the order of the items alphabetically.

Output

In the above output, the list of dictionaries has been sorted according to the “name” and “age” accordingly.

## Conclusion

The “sorted()” function is used to sort the strings, lists, tuples, etc. in ascending or descending order or based on the single or multiple “key” parameter values. This article provides an overview of Python’s “sorted()” function, its parameters, and return value along with numerous examples.

Source: linuxhint.com