How to Sort a Set in Python?

In Python, the “set” is used to store an unordered collection of specific items. To sort the set in a particular order, the “sort()” function or the “sorted()” function is used in Python. Using these methods, you can sort a set in increasing/ascending or decreasing/descending order.

This post will cover the below-mentioned methods that will help you to sort a set in Python:

Method 1: Using sorted() Function

In Python, the “sorted()” function retrieves the list element in ascending order by default.

Example 1: Sort a Set in Ascending Order

The below example code shows the sorted set value:

Code:

set_value = {4,6,8,9,2}
print(sorted(set_value))

In the above code, the “sorted()” function accepts the set as an argument and returns the sorted list in ascending order.

Note: The “sorted()” function will return only a sorted list. Converting the sorted list into sets will change the order of items.

Output:

The above output shows that the input set has been sorted in ascending order but returns as a list.

Example 2: Sort a Set in Descending Order

To sort a set in descending order, the “reverse” parameter must be used in the stated function, as shown in below code:

Code:

set_value = {4,6,8,9,2}
print(sorted(set_value, reverse=True))

In the above code, the “sorted()” function accepts the parameter “reverse=True” to sort the set in descending order. The “sorted()” function will return the iterable value in the list.

Output:

This output shows that the input set has been sorted by the “sorted()” function in descending order.

Method 2: Sort a Set Containing Strings Using sorted() Function

The “sorted()” function can also be used to sort the set containing string values.

Example 1: Using Default Parameters

In the below code, the “sorted()” function is used to sort the set:

Code:

set_value = {'Maradona', 'Lily', 'John', 'Ziyech'}
print(sorted(set_value))

In the above code, the “sorted()” function accepts only the string variable as an argument and retrieves the sorted list based on alphabetical order.

Output:

The input set has been sorted in alphabetical order and returned as a list.

Example 2: Using KeyParameters

In the below code, the parameter “key” is used to sort the set based on specific key value:

Code:

set_value = {'Maradona', 'Lily', 'John', 'Ziyech'}
print(sorted(set_value, key=len))

In the above code, the “sorted()” function accepts the “key=len” as an argument and returns the sorted “list” according to the length of the strings.

Output:

The output authenticates the working of the sorted() function.

Method 3: Using sort() Function

The “sort()” function is used in Python to sort the set items in ascending or descending order.

Example 1: Sort a Set in Ascending Order

The below-given code is used to sort the set items in ascending order:

Code:

set_value = {4,6,8,9,2}
set_to_list = list(set_value)
set_to_list.sort()
print(set_to_list)

In the above code, the “list()” function converts the set value into a list. The “list.sort()” function sorts the original list using the “dot syntax” and doesn’t return anything.

Output:

From the above output, it is clear that the input set has been sorted in ascending order.

Example 2: Sort a Set in Descending Order

To sort a set in descending order, we need to pass the “reverse=True” as an argument in the “list.sort()” function.

Code:

set_value = {4,6,8,9,2}
set_to_list = list(set_value)
set_to_list.sort(reverse=True)
print(set_to_list)

Here, the “list.sort()” function will return the “set” in descending order by accepting the “reverse=True” argument.

Output:

This output shows that the input set has been sorted in descending order via the “list.sort()” function.

Conclusion

To sort a set, the “sorted()” and the “list.sort()” functions are used in Python. These functions sort the set elements and retrieve them as a list. The “sorted()” function is also used to sort the set containing the string value. The “sorted()” function accepts the parameter value “reverse=True” as a parameter and retrieves the sorted list in descending order. The “key” parameter is used in Python to sort the string according to length. This post demonstrated how to sort a set in Python via different methods.