Sort Set of Values in Python

How to sort set values in Python. To sort elements in ascending or descending order use sorted() functions. By using this function you can sort the set of values by ascending, descending, and in the custom order. Set in python is a one-dimensional data structure that will not allow duplicate entries.

1. Quick Examples of Sorting Values in Set

Following are quick examples of how to sort elements in ascending or descending order in Set.


# Below are quick examples

# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})

# Example 1: Sort the elements in the set.
print("Sorted Set: ",sorted(myset))

# Example 2: Sort set in reverse order.
print("Sorted Set: ",sorted(myset,reverse=True))

# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})

# Example 3: Sort the elements in the set.
print("Sorted Set: ",sorted(myset))

# Example 3: Sort elements in custom order
print("Sorted Set: ",sorted(myset,key=len))

2. Python Sort Set using sorted()

The sorted() function in python is used to sort the Set of values in ascending or descending order. This method can be used with python data structures like List, Tuple, and Set. It takes three parameters and returns the sorted elements as a list. By default, the elements in the set are sorted in ascending order.

2.1 Set sorted() Syntax

Following is the syntax of the sorted() function.


#sorted() syntax
sorted(myset,key,reverse=False)

2.2 Set sorted() – Parameters

  1. The first parameter is the input set.
  2. The key is the optional parameter that takes a user-defined or inbuilt function such that the elements in the set are sorted based on the function return elements.
  3. The reverse is an optional parameter that will sort the elements in the set in ascending order when it is set to False, If it is set to True, it will sort in Descending order. By default, it is False.

3. Python Sort Values in Ascending Order

First, let’s sort the python set of values or elements in ascending order. I will use different examples that cover all parameters separately. In the below example, I have created a Set that holds 8 integers and takes the first argument as a set. This returns a list with the elements in ascending order.


# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})
print("Actual Set: ",myset)

# Sort the elements in the set.
print("Sorted Set: ",sorted(myset))

# Output:
# Actual Set:  {32, 67, 6, 12, 78, 54, 56, 90}
# Sorted Set:  [6, 12, 32, 54, 56, 67, 78, 90]

Let’s also sort the set with the string values. This sorts the elements in alphabetical order and returns the result as a list.


# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})
print("Actual Set: ",myset)

# Sort the elements in the set.
print("Sorted Set: ",sorted(myset))

# Output:
# Actual Set:  {'to', 'Examples', 'sparkby', 'welcome', 'hello'}
# Sorted Set:  ['Examples', 'hello', 'sparkby', 'to', 'welcome']

4. Sort Values in Descending Order

By using the same sorted() function in Python we can also sort the set of elements or values in descending order. To do so, you need to pass the reverse param with the value True to the function.


# Consider the set with integers
myset=set({12,32,6,56,78,90,54,67})
print("Actual Set: ",myset)

# Sort the elements in the set in reverse order.
print("Sorted Set: ",sorted(myset,reverse=True))

# Output:
# Actual Set:  {32, 67, 6, 12, 78, 54, 56, 90}
# Sorted Set:  [90, 78, 67, 56, 54, 32, 12, 6]

You can see that all the elements are sorted in descending order because we specified reverse to True. Let’s see an example with a String.


# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})
print("Actual Set: ",myset)

# Sort the elements in the set in ascending order.
print("Sorted Set: ",sorted(myset,reverse=True))

# Output:
# Actual Set:  {'to', 'Examples', 'sparkby', 'welcome', 'hello'}
# Sorted Set:  ['welcome', 'to', 'sparkby', 'hello', 'Examples']

5. Sort Set in Custom Order

Let’s sort the elements in the set by specifying the key parameter. Here we will specify the in-built method – len. So the elements are sorted based on the length of the string.


# Consider the set with strings
myset=set({"hello","welcome","to","sparkby","Examples"})
print("Actual Set: ",myset)

# Sort the elements in the set based on length of the string.
print("Sorted Set: ",sorted(myset,key=len))

# Output:
# Actual Set:  {'to', 'Examples', 'sparkby', 'welcome', 'hello'}
# Sorted Set:  ['to', 'hello', 'sparkby', 'welcome', 'Examples']

You can see that all the elements are sorted based on the string length in ascending order.

3. Conclusion

We have seen how to sort the elements in a set using the Python sorted() method. It will return the sorted elements in a list. In our entire article, we explained this method by considering all the parameters. You can also use in-built or custom methods in key parameters. By default, it performs in ascending order, to sort in descending order use reverse=False.

Leave a Reply