Python Program for Set sorted() Method

In the previous article, we have discussed Python Program for Set remove() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set = {'hello', 'this', 'is', 'btechgeeks'}

Output:

The given set is :
{'is', 'hello', 'btechgeeks', 'this'}
The given set after sorting the items in ascending order :
['btechgeeks', 'hello', 'is', 'this']

Explanation:

Here the items of the set are sorted in ascending order based upon the ASCII values.

Example2:

Input:

 Given set = {2, 3, 5, 10, 6, 12, 50, 1}

Output:

The given set is :
{1, 2, 3, 5, 6, 10, 12, 50}
The given set after sorting the items in descending order :
[50, 12, 10, 6, 5, 3, 2, 1]

Explanation:

Here the items of the set are sorted in descending order using the reverse= True condition
(based upon the ASCII values)

Program for Set sorted() Method in Python

set sorted() Method:

One of the set methods in Python is the set sorted() function, which is used to sort the given set items in ascending or descending order.

Syntax:

sorted(iterable,key,reverse)  

ex: sorted(setname) - for ascending order
     sorted(setname, reverse=True)- for descending order.

Parameter Values:

sorted() accepts three parameters, two of which are optional.

Iterable: a sequence (list, tuple, or string) or a collection (dictionary, set, or frozen set), or any other iterator that must be sorted.
Key(optional): A function that can be used as a key or as the basis for sort comparison.
Reverse(optional): If set to true, the iterable is sorted in reverse (descending) order; otherwise, it is set to false.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Apply sorted() method to the given set to sort the items of the given set either in ascending or descending order.
  • Store it in another variable.
  • Print the given set after sorting all the items in ascending order.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is', 'btechgeeks'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply sorted() method to the given set to sort the items of the given set either in
# ascending or descending order.
# Store it in another variable.
sortd_set = sorted(gven_set)
# Print the given set after sorting all the items in ascending order.
print("The given set after sorting the items in ascending order :")
print(sortd_set)

Output:

The given set is :
{'this', 'hello', 'is', 'btechgeeks'}
The given set after sorting the items:
['btechgeeks', 'hello', 'is', 'this']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Apply sorted() method to the given set to sort the items of the given set either in ascending or descending order.
  • Store it in another variable.
  • Print the given set after sorting all the items in descending order.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(),map(), input(), and split() functions.
# Store it in a variable.
gven_set = set(map
(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Apply sorted() method to the given set to sort the items of the given set either in
# ascending or descending order.
# Store it in another variable.
sortd_set = sorted(gven_set, reverse=True)
# Print the given set after sorting all the items in descending order.
print("The given set after sorting the items in descending order :")
print(sortd_set)

Output:

Enter some random values separated by spaces = 2 3 5 10 6 12 50 1
The given set is :
{1, 2, 3, 5, 6, 10, 12, 50}
The given set after sorting the items in descending order :
[50, 12, 10, 6, 5, 3, 2, 1]

Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples tutorial will definitely aid programmers to learn easily & efficiently.