Summary: This blog explores the steps to sort elements of a Set.
Python offers the built-in sorted()
function to sort elements in container objects such as a set or a list. For example: sorted({1, 5, 2})
sorts the elements in the set and returns the sorted list [1, 2, 5]
.
Note: All the solutions provided below have been verified using Python 3.9.0b5
Problem Formulation
Imagine that one has the following Set in Python.
set(['4.928857000', '0.030778000', '4.927327899', '0.002949589', '0.023685000', '11.463524000', '0.0270662958'])
Desired Output:
How does one sort this Set, so that the output is the following List.
['0.002949589', '0.023685000', '0.0270662958', '0.030778000', '4.927327899', '4.928857000', '11.463524000']
Background
Wikipedia: “In computer science, a Set is an abstract data type that can store unique values, without any particular order.”
Python defines a Set as an unordered collection with no duplicate elements. In Python, a Set is a built-in data-type that stores collections of data. Think of a Set as container type, similar to lists, tuples and dictionaries. Python programmers use sets to test for membership and to remove duplicate items.
How to Sort a Set?
Since 2013, the Python Community has asked the question about sorting a Set, about 260k times. That is incredible, given the above background of a Set.
So first things first! In Python, Elements in a given Set, do not have any specific order. One can iterate thru each element in a Set and sort the resulting output. Python offers the built-in sorted() function to sort elements in container objects such as a Set or a List. The following code examples show how to use the sorted()
function.
$ python Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) >>> >>> ## This is the original Set Object whose elements will be sorted. >>> my_set = set(['4.928857000', '0.030778000', '4.927327899', '0.002949589', '0.023685000', '11.463524000', '0.0270662958']) >>> >>> ## This is the type of my_set. Yes, Python confirms, it is a set!! >>> type(my_set) <class 'set'> >>> >>> ## The Python Built-in function “sorted()” iterates thru the elements in the >>> ## given set argument and sorts them. >>> my_sorted_elements = sorted(my_set) >>> >>> ## By default, the sorted() function assumes the following… >>> ## The elements are strings >>> ## The sorting is done in ascending order. >>> my_sorted_elements ['0.002949589', '0.023685000', '0.0270662958', '0.030778000', '11.463524000', '4.927327899', '4.928857000'] >>> >>> ## The sorted() function sorts the elements and returns a list object. >>> type(my_sorted_elements) <class 'list'> >>>
The astute reader may have noticed above, that the sorting order is funky. That is: ‘0.030778000’, ‘11.463524000’, ‘4.927327899’
This is because the sorted()
function assumes the set elements are strings, by default. Consider the first element in the my_sorted_elements
list.
>>> my_sorted_elements[0] '0.002949589' >>> >>> type(my_sorted_elements[0]) <class 'str'> >>>
How to Sort Elements As Floats?
It is important to note that the sorted elements in the returned list are still strings. The sorted() function converts the elements into floats. It does this for the sole purpose of sorting them. After it sorts the elements, the sorted() function reverts them back to be string elements. The following code example shows how to sort the Set elements as floats.
$ python Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) >>> >>> ## This is the original Set Object whose elements will be sorted. >>> my_set = set(['4.928857000', '0.030778000', '4.927327899', '0.002949589', '0.023685000', '11.463524000', '0.0270662958']) >>> >>> ## The Python Built-in function “sorted()” iterates thru the elements in the >>> ## given my_set argument and sorts them. This time as floats. >>> my_sorted_elements = sorted(my_set, key=float) >>> >>> ## It should be noted that the elements in the returned list are still strings. >>> ## The sorted() function converts the elements into floats, for the sole purpose >>> ## of sorting them, then reverts them back to be string elements >>> my_sorted_elements ['0.002949589', '0.023685000', '0.0270662958', '0.030778000', '4.927327899', '4.928857000', '11.463524000'] >>> >>> my_sorted_elements[0] '0.002949589' >>> type(my_sorted_elements[0]) <class 'str'> >>> >>> ## my_sorted_elements continues to be a list object. >>> type(my_sorted_elements) <class 'list'> >>>
Can The Sorting Order Be Changed?
The sorted()
built-in function also offers the ‘reverse’ argument, to change the sorting order. Setting reverse=True, changes the sorting order to be descending. The following code shows how to change the sorting order to descending.
$ python Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) >>> >>> ## This is the original Set Object whose elements will be sorted. >>> my_set = set(['4.928857000', '0.030778000', '4.927327899', '0.002949589', '0.023685000', '11.463524000', '0.0270662958']) >>> >>> ## As before, the Python Built-in “sorted()” function iterates thru the elements in the >>> ## given my_set argument and sorts them as floats. This is now in done in descending >>> ## order, because of the ‘reverse’ argument. >>> my_sorted_elements = sorted(my_set, key=float, reverse=True) >>> >>> ## Also as before, the elements in the returned list are still strings. >>> ## Also as before, the sorted() function converts the elements into floats, for the >>> ## sole purpose of sorting them, then reverts them back to be string elements >>> my_sorted_elements ['11.463524000', '4.928857000', '4.927327899', '0.030778000', '0.0270662958', '0.023685000', '0.002949589'] >>> >>> my_sorted_elements[0] '11.463524000' >>> type(my_sorted_elements[0]) <class 'str'> >>> >>> ## Again as before, my_sorted_elements continues to be a list object. >>> type(my_sorted_elements) <class 'list'> >>>
What Happens If The Original Set Elements Are Floats?
The process remains the same if the original Set elements are floats. One does not need to provide the ‘key’ argument. Python is smart enough to recognize the elements as floats. Consider the following code.
$ python Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) >>> >>> ## This is the original Set Object whose elements will be sorted. This time the elements >>> ## are floats. >>> my_set = set([4.928857000, 0.030778000, 4.927327899, 0.002949589, 0.023685000, 11.463524000, 0.0270662958]) >>> >>> type(my_set) <class 'set'> >>> ## Note that the ‘key’ argument is not needed, to sort as floats. >>> my_sorted_elements = sorted(my_set) >>> >>> ## Python recognizes the set elements as floats and sorts them in ascending order >>> my_sorted_elements [0.002949589, 0.023685, 0.0270662958, 0.030778, 4.927327899, 4.928857, 11.463524] >>> >>> ## As before, my_sorted_elements continues to be a list object. >>> type(my_sorted_elements) <class 'list'> >>> >>> ## Unlike the earlier sections, the elements in the my_sorted_elements list are >>> ## actually floats. >>> my_sorted_elements[0] 0.002949589 >>> type(my_sorted_elements[0]) <class 'float'> >>>
What About Sorting In Descending Order?
Python is a very intuitive scripting language. If there is sorting in ascending order, one can be sure there is a way to sort in descending order too. The ‘reverse’ argument tells the sorted() function to sort in descending order. Consider the following code.
$ python Python 3.9.0b5 (default, Oct 19 2020, 11:11:59) >>> >>> ## This is the original Set Object whose elements will be sorted. As above, the >>> ## elements are float values. >>> my_set = set([4.928857000, 0.030778000, 4.927327899, 0.002949589, 0.023685000, 11.463524000, 0.0270662958]) >>> >>> type(my_set) <class 'set'> >>> ## Note that the ‘key’ argument is not needed, to sort as floats. >>> my_sorted_elements = sorted(my_set, reverse=True) >>> >>> ## Python recognizes the set elements as floats and sorts them in ascending order >>> my_sorted_elements [11.463524, 4.928857, 4.927327899, 0.030778, 0.0270662958, 0.023685, 0.002949589] >>> >>> ## As before, my_sorted_elements continues to be a list object. >>> type(my_sorted_elements) <class 'list'> >>> >>> ## Unlike the earlier sections, the elements in the my_sorted_elements list are >>> ## actually floats. >>> my_sorted_elements[0] 11.463524 >>> type(my_sorted_elements[0]) <class 'float'> >>>
Conclusion
In Python, the Set object contains unordered elements. But, one can use the sorted() built-in function to sort its elements. The original Set itself remains unchanged. The sorted() function returns a List object with the sorted elements. The sorted() function can take various arguments. These arguments specify the type of elements and/or the sorting order.
Finxter Academy
This blog was brought to you by Girish Rao, a student of Finxter Academy. You can find his Upwork profile here.
Reference
All research for this blog article was done using Python Documents, the Google Search Engine and the shared knowledge-base of the Finxter Academy and the Stack Overflow Communities.