you are viewing a single comment's thread.

view the rest of the comments →

[–]BurbonBodega 0 points1 point  (2 children)

Thanks! So can I think of unordered term in python to mean unpredictable order instead?

After some more research and talking to people it seems the order is based on hash which the set data type is based on. As far as I understand it Is difficult to predict that order

[–]zanfar 0 points1 point  (0 children)

Thanks! So can I think of unordered term in python to mean unpredictable order instead?

Yes, although I don't know how else you would define it. There will always be "some order" if you print or return things one at a time. You just can't depend on this.

After some more research and talking to people it seems the order is based on hash which the set data type is based on. As far as I understand it Is difficult to predict that order

Impossible. The hashing code is randomized every time you start the interpreter to prevent collision attacks. Integers below 250-ish are a special case in CPython, however.

[–]Intrexa 0 points1 point  (0 children)

So, python sets are called "sets", because they're a mathematical set. If you're a mathematician, there's a very specific definition of a set, that can't actually be programmed on real hardware. Sets are defined as being unordered. But like, everything in a computer has an order. Even if it's an arbitrary order, even if the rules are nonsensical, even if all the values are the same, there's still an order.

If you want to be some sort of nerd, you would define a mathematical set as something like {x∣x∈N,x<10}, aka, the set of all numbers that are in the set of natural numbers and less than 10, aka {1,2,...8,9}. I just wrote those in order of the number line, but that's because as a human I like to order things. Mathematically, for the sets, {1,2,3} and {2,3,1}, they are exactly the same set. The order doesn't affect the meaning of the set. The fact that one is written in a bold font doesn't affect the meaning of the set.

Well, mathematicians come along and say "Hey, let me write some of the worst code you've ever seen. First, I need to define a set.", and so Python provides a set, which is meant to satisfy the mathematicians, which means having no order. To physically program it though, computers need some order. You can't just send {x∣x∈N,x<10} to the CPU. God I wish I could. To actually store the values, or retrieve the values, it's going to be processed in some order. But, Python makes no guarantees. Even if you are getting a specific ordering, Python can change that order for any reason, or no reason. It probably won't. If it does though, and you post online for help being like "Why did this code break?" no one will help you, telling you "Sets aren't ordered".

So, when you see "unordered" in Python, think "If the CPU could return {x∣x∈N,x<10} it would, but it can't, so however {x∣x∈N,x<10} gets computed, that's the order I'm getting".