1
s = {1,1,2,2,3,2,1,2,4,3,5,8}
s.add(7)
print(s)


#the output is   
{1, 2, 3, 4, 5, 7, 8}.



However , for 
s = {1,1,2,2,3,2,1,2,4,3,5,100}
s.add(7)
print(s)

#the output is  
{1, 2, 3, 4, 100, 5, 7}.

My question: why is it that in the first case, the '7' is added to the set so that the set is ordered in ascending order, whereas in the second case, it is added to the end of the set?

Sush R
  • 13
  • 4
  • _“the end of the set”_ > as sets are unordered, the concept of the end of the set does not exist. – spectras Oct 23 '16 at 15:37

1 Answers1

0

Sets are unordered. Keep constructing copies of the first set, see if it maintains the numeric order. Try making the set a little more populated, generate a bunch of them in the same session. Try making a really big one... Hack @it :)

MmmHmm
  • 3,435
  • 2
  • 27
  • 49
  • so, does that mean that elements that are added to sets are placed randomly in the set? – Sush R Oct 23 '16 at 15:38
  • @SushR No, I don't think random is the most accurate description as there are specific particulars which will somewhat determine the arrangement of Set values. I think the win is that Sets are efficient because of their non-redundancy and like hashes, the non-ordered aspect is related to the speed of use and memory concerns. See the video and the answers [here](http://stackoverflow.com/questions/12165200/order-of-unordered-python-sets). Hope that helps! – MmmHmm Oct 23 '16 at 22:09
  • @SushR in particular, per the [video@27m57s](https://youtu.be/C4Kc8xzcA68?t=27m57s), "A set is a dictionary without storage for the value" – MmmHmm Oct 23 '16 at 23:22