Sorting set in python

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.

) # 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() # 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.

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.

) print("Actual Set: ",myset) # Sort the elements in the set. print("Sorted Set: ",sorted(myset)) # Output: # Actual Set: # 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.

) print("Actual Set: ",myset) # Sort the elements in the set. print("Sorted Set: ",sorted(myset)) # Output: # Actual Set: # 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.

) print("Actual Set: ",myset) # Sort the elements in the set in reverse order. print("Sorted Set: ",sorted(myset,reverse=True)) # Output: # Actual Set: # 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.

) print("Actual Set: ",myset) # Sort the elements in the set in ascending order. print("Sorted Set: ",sorted(myset,reverse=True)) # Output: # Actual Set: # 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.

) 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: # 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 .

  • How to Sort a List of Tuples in Python
  • Python Sort List Descending
  • Python Sort Dictionary by Key
  • Python Sort Array Values
  • Python Sort List in Reverse Order
  • Python Sort List of Numbers or Integers
  • Python Sort List Alphabetically
  • Sort using Lambda in Python
  • How to Sort List of Strings in Python
  • How to Sort Dictionary by Value in Python
  • Python Sort List of Lists

References

You may also like reading:

Источник

Sort a Set in Python

Sort a Set in Python

  1. Use the sorted() Function to Sort a Set in Python
  2. Use the sort() Method to Sort a Set in Python

Sets are an unordered and unindexed collection having no duplicate elements. Sets are one of the four built-in data types available in Python and are written using curly brackets.

Given that sets are unordered, it is not possible to sort the values of a set. However, if we print a set, it is displayed in a sorted manner.

If we want, we can use different functions will sort a set and return a list. These are discussed below.

Use the sorted() Function to Sort a Set in Python

The sorted() function is a built-in function in Python that returns a sorted sequence (list,tuple,string) or sorted collection(sets,dictionary) in the form of a list. The sorted() function has no effect on the original iterable sequence as it provides a new sorted output.

Note that the sorted() function returns a list instead of a set. We can again convert this list to a set using the set() function. However, this is redundant since sets are unordered.

Use the sort() Method to Sort a Set in Python

The sort() Method is used to sort the elements given in a list in ascending or descending order. It is a destructive process, i.e., unlike the sorted() function, the sort() method sorts the elements in the original list itself.

We cannot use this function with a set. However, we can enclose a set within a list, and it will sort them.

Источник

Читайте также:  Php var dump аналог
Оцените статью