close

How to Sort a Set in Python

Hello guys. How are you? Hope you all fine. Here we are going to learn about to sort the set in python. There are two methods to sort the set which we will learn today. first one is to sort set by using sort() function and second is by using sorted(). so lets go and learn How to Sort a Set in Python.

How to Sort a Set in Python

  1. to Sort a Set in Python

    using sort() You can sort the set by using sort() function. It will sort the set. You can see this in below example :set1 = [{8,6,4,7,9,}] set1.sort() print(set1)Output :
    [{4, 6, 7, 8, 9}] Thus sort the set by just using sort().

  2. How to Sort a Set in Python

    using sorted() You can sort the set by using sorted() function. It will sort the set. You can see this in below example :set1 = {'c','g','b','t','z','a'}print(sorted(set1)) set1 ={'0.00023','0.0056','0.0085','0.0045','0.0012','0.0099'} print(sorted(set1))
    Output :['a', 'b', 'c', 'g', 't', 'z'] ['0.00023', '0.0012', '0.0045', '0.0056', '0.0085', '0.0099']Thus sort the set by just using sorted().

Method 1 :using sort()

You can sort the set by using sort() function. It will sort the set. You can see this in below example :

set1 = [{8,6,4,7,9,}]
set1.sort()
print(set1)

Output :

[{4, 6, 7, 8, 9}]

Thus sort the set by just using sort().

Method 2 : using sorted()

You can sort the set by using sorted() function. It will sort the set. You can see this in below example :

set1 = {'c','g','b','t','z','a'}
print(sorted(set1))
set1 = {'0.00023','0.0056','0.0085','0.0045','0.0012','0.0099'}
print(sorted(set1))

Output :

['a', 'b', 'c', 'g', 't', 'z']
['0.00023', '0.0012', '0.0045', '0.0056', '0.0085', '0.0099']

Thus sort the set by just using sorted().

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment