Lets say you have a set:
foo = {1, 2, 3, 4, 5}
In the book I am currently reading, Pro Python, it says that using foo.pop()will pop an arbitrary number from that selection. BUT…When I try it out, it pops 1, then 2, then 3...Does it do it arbitrarily, or is this just a coincidence?
The reason it says it is arbitrary is because there is no guarantee about the ordering it will pop out. Since you just created the set, it may be storing the elements in a “nice” order, and thus
.pop()happens to return them in that order, but if you were to mutate the set, that might not continue to hold.Example: