0

Here is my code: i used set() and it return [3, 14, 6]

items = [3, 6, 3, 3, 14]
set(items)
>>> set([3,14,6])

My question is how is the set function organizes it values output. If we think about this, 3 is the first number and 6 is the second on the list, so should it output [3,6,14] instead?

tipsywacky
  • 3,374
  • 5
  • 43
  • 75
  • possible duplicate of [python set changes element order?](http://stackoverflow.com/questions/9792664/python-set-changes-element-order) – mmmmmm Mar 01 '14 at 16:31

2 Answers2

2

Sets are unordered. From the documentation:

Being an unordered collection, sets do not record element position or order of insertion.

Like dictionaries, the ordering is based on the hashes of the stored keys. You cannot rely on this apparent ordering to remain stable.

If you are interested in the underlying data model, the underlying data structure is called a Hash Table, but in sets only keys are stored, values are left empty.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • ah I see, but so you have to use other functions like sort to organize the number then? One more question though, when you re-compile a program and print hashes, I can see the values are being shuffled sometimes, is that the same case with the set function?Correct me if I'm wrong. Thanks! – tipsywacky Nov 16 '12 at 08:33
  • 1
    Note that, there are other possible implementations of storing a set like, e.g. a balanced tree. This is an implementation issue, and no assumptions should be made on the order of a set, no matter how it is implemented. This is the meaning of unordered, that no assumptions should be made regarding its order. – Dov Grobgeld Nov 16 '12 at 08:36
  • 1
    @tipsywacky: You cannot sort the contents of the set. When using the contents, you can obtain a sorted sequence of those contents, but then you'll be handling a list instead. That's fine for certain operations of course. – Martijn Pieters Nov 16 '12 at 08:43
1

@Martijn has given you the reason why, but just a couple more bits that might be useful:

You can use a dict with value as key and the position from your original list as value:

d = dict( (val, idx) for idx, val in enumerate(items) )
# or {val:idx for idx, val in enumerate(items)} in 2.7+
print d.keys() # unique values: [3, 14, 6]
print sorted(d) # unique values in order [3, 6, 14]
print sorted(d, key=d.get) # unique values in original order (based on last occurence of key [6, 3, 14]

And slightly a bit more work to get original order, based on first occurence:

d = {}
for idx, val in enumerate(items):
    d.setdefault(val, idx)
Jon Clements
  • 138,671
  • 33
  • 247
  • 280