I don't understand why set() works the way it does...
Let's say we have two lists:
a = [1,2,-1,20,6,210,1, -11.4, 2]
b = [1,2,-1,20,6,210,1,-11.4, 2, "a"]
When I run set() on the list of numerics, a, I get a set of unique numerics ordered from smallest to largest. Ok great, that seems intuitive! Haven't found any exceptions yet:
set(a)
Out: {-11.4, -1, 1, 2, 6, 20, 210}
What happens if I throw a character in like with list b? Weirdness. The negatives are out of order and so is 6.
set(b)
Out: {-1, -11.4, 1, 2, 20, 210, 6, 'a'}
It gets worse though. What if I try to turn those sets back into lists? Pure chaos.
list(set(a))
Out: [1, 2, 6, 210, 20, -11.4, -1]
list(set(b))
Out: [1, 2, 6, 'a', 210, 20, -11.4, -1]
As you can see, these lists indeed only have unique values. But have failed to preserve much semblance of the order of the original lists.
What's going on here and why?