The hash()
function of python does a predefined set of operations to come up with its value. What those operations are is further explained here: A given object (string, integer, whatever) will always yield the same hash value.
When you put items into a set (or similar structure), they are rehashed whenever the size of the set reaches a certain threshold. Thus, while you may be unable to predict what order a certain set of items would be in, the same n items will always be in the same order in a set.
Thus, effectively yes... a,b,c,d,e,f,g
, where each is a specific string or integer, would always appear in the same order when iterated through in a set. (though, not necessarily the order I just listed them).
EDIT: Edited for clarity based on comments.
EDIT: Console Proof
Ran under python 2.5 on Debian 32bit, python 3 on 64bit and 2.7 on Windows XP 32bit.. comes out the same in all of them, and I've used the fact in programs before with no problems.
Thanks to Chris for the additional platforms to confirm test.
>>> test = ['cat', 'dog', 'mouse', 'rat', 6126, 516]
>>> temp = []
>>> for x in set(test):
temp.append(x)
>>> temp
[516, 'dog', 6126, 'cat', 'rat', 'mouse']
>>> temp = []
>>> for x in set(test):
temp.append(x)
>>> temp
[516, 'dog', 6126, 'cat', 'rat', 'mouse']
>>>