See Sven's answer, but instead I would use the sorted() function: this way you will get the elements in a good predictable order (for example, you can compare the lists later).
>>> s = set([1, 2, 3, 4, 5]) >>> sorted(s) [1, 2, 3, 4, 5]
Of course, set items must be sortable in order for this to work. You cannot sort complex numbers (see gnibbler Comment). In Python 3, you also cannot sort any set with mixed data types, for example. set([1, 2, 'string']) .
You can use sorted(s, key=str) , but in these cases it may not be worth it.
Petr Viktorin Jun 19 2018-11-11T00: 00Z
source share