thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

set and dict are not ordered

β€’ 61 words β€’ 1 min β€’ updated

Sets and dictionaries are not ordered.

python
>>> for n in set([3,1,2,-1]): print(n)
...
1
2
3
-1

If you want the set to be ordered, use sorted:

python
>>> for n in sorted(set([3,1,2,-1])): print(n)
...
-1
1
2
3

This is unlike C++’s set:

std::set is an associative container that contains a sorted set of unique objects of type Key.