Python splat/unpacking/* operator on sets: is the result always sorted?

353 Views Asked by At

Does the splat/unpacking operator * always sort when applied to sets of ints?

Example 1 (ints):

In [1]: [*{8,3,6,3,2,1}]
Out[1]: [1, 2, 3, 6, 8]

Counter Example 2 (strings):

In [615]: [*{'b','d','a','c','e'}]
Out[615]: ['a', 'd', 'c', 'b', 'e']

Counter example 3 (tuples):

In [1]: [*{(2,3),(2,2),(1,2),(1,1)}]
Out[2]: [(2, 3), (1, 1), (1, 2), (2, 2)]
1

There are 1 best solutions below

1
Tranbi On BEST ANSWER

No it's not

print([*{8,3,6,3,2,1,108,109,255,569}])

gives me:

[1, 2, 3, 6, 8, 108, 109, 569, 255]

You cannot sort sets. If you need to sort them you'll have to convert them to list or use a specific module like sortedcontainers