How to sort a set in python

hello guys. How are you all? In this tutorial we will learn about How to sort a set in python. We can sort a set on different methods like increasing , decreasing method and you can also sort it on index or alphabet. The other method is you can also sort a set on the basis of the length of the elements in it. So here we are discuss abut of how to sort a set by different methods. I hope you guys like it very much. So without wasting your time lets learn about of this by given below methods.

How to sort a set in python

Advertisements
  1. sort a set in python

    to sort a set in python just Use sorted().By using sorted () function you can sort a set in python. First of all make a set with a name. Here i have myset named set. Then use the function sorted with this set and it will sort your set. Lets learn this by given example: myset = {18,15,14,18,20,11} print(sorted(myset)) Output : [11, 14, 15, 18, 20] You can also sort the set on alphabet wise and also can sort it out on index wise. So lets learn alphabet wise. myset = {'eye','apple','fish','bat','cat','dog'} print(sorted(myset)) Output :
    ['apple', 'bat', 'cat', 'dog', 'eye', 'fish']

  2. How to sort a set in python

    to sort a set in python just Use sort(). sort() function used for to sort the set in python. First of all make a set. Here i have myset named set then use it with sort() function and then print it. It will sort out your set. Lets learn this by example: myset = [{18,15,14,18,20,11}] myset.sort() print(myset) Output : [{18, 20, 11, 14, 15}] You can also set it on alphabet wise lets sort it. myset = [{'eye','apple','fish','bat','cat','dog'}] myset.sort() print(myset) Output : [{'apple', 'dog', 'cat', 'bat', 'eye', 'fish'}]

Method 1:Use sorted()

Advertisements

By using sorted () function you can sort a set in python. First of all make a set with a name. Here i have myset named set. Then use the function sorted with this set and it will sort your set. Lets learn this by given example:

myset = {18,15,14,18,20,11}
print(sorted(myset))

Output :

[11, 14, 15, 18, 20]

You can also sort the set on alphabet wise and also can sort it out on index wise. So lets learn alphabet wise.

myset = {'eye','apple','fish','bat','cat','dog'}
print(sorted(myset))

Output :

['apple', 'bat', 'cat', 'dog', 'eye', 'fish']

Method 2: Use sort()

Advertisements

sort() function used for to sort the set in python. First of all make a set. Here i have myset named set then use it with sort() function and then print it. It will sort out your set. Lets learn this by example:

myset = [{18,15,14,18,20,11}]
myset.sort()
print(myset)

Output :

[{18, 20, 11, 14, 15}]

You can also set it on alphabet wise lets sort it.

myset = [{'eye','apple','fish','bat','cat','dog'}]
myset.sort()
print(myset)

Output :

[{'apple', 'dog', 'cat', 'bat', 'eye', 'fish'}]

Method 3: Use len()

Advertisements

len() function is used for to sort the set on the bases of the length of the elements. You can understand it better by given below example. So without wasting your time lets learn this.

myset = {'eye','apple','fish','bat','cat','dog'}
print("Sorted list:", sorted(myset))
print("Sorted on len parameter:", sorted(myset, key=len))

Output :

Sorted list: ['apple', 'bat', 'cat', 'dog', 'eye', 'fish']
Sorted on len parameter: ['dog', 'bat', 'eye', 'cat', 'fish', 'apple']

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment