Python is a flexible and well-liked programming language that provides a variety of built-in capabilities to make programming duties simpler. One such operate is the “sorted()” operate, which lets you type varied forms of information in both “ascending” or “descending” order which assists particularly in mathematical computations.
This text will clarify the working of Python’s “sorted()” operate to reverse the sorted information.
What’s the “sorted()” Perform in Python?
The “sorted()” operate in Python is utilized to type varied forms of information, together with strings, lists, and tuples, in both ascending or descending order. The operate accepts an iterable as a parameter/argument and retrieves a brand new sorted listing.
Syntax
sorted(iterable, key=None, reverse=False)
Parameters
The “sorted()” operate takes the next parameters:
- “iterable”: This parameter specifies the iterable that we need to type, reminiscent of a string, listing, or tuple.
- “key”: That is an non-compulsory argument that determines a operate to be known as/accessed on every merchandise of the iterable. The operate is used to extract a comparability key from every factor, which is then used to type the iterable.
- “reverse”: This non-compulsory parameter specifies whether or not the iterable needs to be sorted in reverse order. Usually, it’s set to “False”, which signifies that the enter iterable is sorted in ascending order.
Return Worth
The “sorted()” operate retrieves a singular listing with the iterable’s parts in sorted order.
Instance 1: Making use of the “sorted()” Perform to Type String, Record, and Tuple
Let’s check out the under code utilizing the “sorted()” operate to type the mentioned sorts:
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)
Within the above code, the created string, listing, and tuple are sorted utilizing the utilized “sorted()” operate in every case, accordingly.
Output
From the above snippet, it may be seen that the weather of the “string”, “listing”, and “tuple” are sorted in ascending order.
Instance 2: Making use of the “sorted()” Perform to Type the Enter Record in Ascending Order
The “sorted()” operate can be utilized to explicitly type parts of an iterable in ascending order, as follows:
list_value = [99, 45, 22, 35]
sorted_list = sorted(list_value, reverse=False)
print(‘Given Record: ‘, list_value)
print(‘Sorted Record: ‘, sorted_list)
Within the above instance, explicitly set the “reverse” parameter of the utilized “sorted()” operate to “False” to type the created listing in ascending order.
Output
Primarily based on the above output, the ascending listing has been produced.
Instance 3: Making use of the “sorted()” Perform to Type the Record in Descending Order
The below-provided code is used to type the offered listing in descending order:
list_value = [29, 45, 12, 35]
sorted_list = sorted(list_value, reverse=True)
print(‘Given Record: ‘, list_value)
print(‘Sorted Record: ‘, sorted_list)
Within the above code traces, the “reverse” parameter is assigned the worth “True” to type the listing in descending order as an alternative. In consequence, the “sorted()” operate returns a singular listing with the weather/objects in reverse order.
Output
The above output verified that the given listing parts have been sorted reversely.
Instance 4: Making use of the “sorted()” Perform With the “Key” Parameter to Type the Record of String
The “key” parameter of the “sorted()” operate can be used to change the way in which objects in a listing are sorted. The operate takes the “key” parameter and returns a worth that’s used to find out the order of the objects:
string_value = [“Joseph”, “Anna”, “Lily”, “Molly”]
sorted_str = sorted(string_value, key=len)
print(‘Given Record of String: ‘, string_value)
print(‘Sorted Record of String: ‘, sorted_str)
On this instance, the “sorted()” operate allocates “len” to the “key” parameter to type the given listing of strings based mostly on the size of the alphabet. The “sorted()” operate then kinds the listing in ascending order based mostly on the size of every phrase.
Output
The given listing of strings has been sorted utilizing the “sorted()” operate.
Instance 5: Making use of the “sorted()” Perform With A number of “Key” Parameters to Type the Record of Dictionary
We are able to additionally use a number of keys to type a given iterable reminiscent of a dict, tuple, or listing. On this case, the dictionary parts/objects are sorted in response to the primary key “identify”, after which the second key “age”. Right here is an instance 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)
Primarily based on the above code:
- The “sorted()” operate kinds the listing of dictionaries based mostly on the “identify” and “age” keys within the given dictionary.
- The “sorted()” operate takes two arguments together with the dictionary identify “dict_value” and a “key” parameter allotted as “lambda”, respectively.
- The lambda operate is utilized because the “key” parameter to type the order of the objects alphabetically.
Output
Within the above output, the listing of dictionaries has been sorted in response to the “identify” and “age” accordingly.
Conclusion
The “sorted()” operate is used to type the strings, lists, tuples, and so on. in ascending or descending order or based mostly on the only or a number of “key” parameter values. This text gives an outline of Python’s “sorted()” operate, its parameters, and return worth together with quite a few examples.