0

When I type s1 = {4,3,8,1,8,2} and check the values it always prints in ordered way as {1, 2, 3, 4, 8}. So is this is not ordered?

As per the duplicate question, even if I run there sample,

list1 = [8,16,24]
set(list1)        #prints {8, 16, 24}
list2 = [24,16,8]
set(list2)        #prints {8, 16, 24}

As per the duplicate question, they get different output. But for me as you can see every time it orders. I'm using Python 3.6.5. Is this how its suppose to behave?

Zoe
  • 27,060
  • 21
  • 118
  • 148
dnWick
  • 393
  • 3
  • 14
  • @jpp this is not a duplicate of that question. For him values are coming unordered. For me its always ordered. I have seen that question. When I try this set set_1 = set([5, 2, 7, 2, 1, 88]) , I get a ordered output as {1, 2, 5, 7, 88} – dnWick Jun 16 '18 at 14:13
  • See duplicate. This is know as a "coincidence". – jpp Jun 16 '18 at 14:16
  • @ jpp can you please be resonable here as for having moderator actions ? I have asked a valid question which every time I create a set in no matter what order , it always prints in order even set([5, 88, 1, 2, 7, 2,55,31,61,456,1234]) prints as {1, 2, 5, 7, 31, 55, 61, 88, 456, 1234}. I'm using python 3x here. – dnWick Jun 16 '18 at 14:27
  • I'm not a moderator. Feel free to click on the "flag" link if you think this post requires moderator attention. – jpp Jun 16 '18 at 14:29
  • 1
    due to an implementation detail of CPython, the set appears ordered... but you can't rely on it being ordered... because sometimes (possibly in other implementations) it may not be. – Corey Goldberg Jun 16 '18 at 14:42
  • 1
    go to http://pyfiddle.io and input `import random` and `print(set( random.sample(range(40000), k = 500)))` - execute the script, review output. Its not ordered. qed. – Patrick Artner Jun 16 '18 at 14:53
  • 1
    It has to do with your python version, check https://docs.python.org/3/whatsnew/3.6.html#new-dict-implementation , `The order is preserved from python3.6` – Stack Jun 16 '18 at 14:56
  • Thank you very much . Yes I' using python 3.6.5. If you put it as an answer i can accept it :) – dnWick Jun 16 '18 at 15:00
  • 3
    @Stack according to `platform.python_version()` pyfiddle.io (if switched to 3.6) returns 3.6.1 and your News are for **dicts** not for sets – Patrick Artner Jun 16 '18 at 15:01
  • 1
    @PatrickArtner **sets are implemented as something like dictionaries with dummy values** according to https://stackoverflow.com/a/3949350/8150371 – Stack Jun 16 '18 at 15:18
  • @stack no idea if the space-saving changes to dicts which since then have an inbuilt ordering now effects the _pure_ set implementation - gotta read that link a bit more carefully, thanks for providing – Patrick Artner Jun 16 '18 at 15:23
  • @dnWick No problem, Can't answer as this has question has been flagged. – Stack Jun 16 '18 at 15:33
  • Oh sorry . I had to flag this. Thanks again ! – dnWick Jun 16 '18 at 15:37
  • A note: IPython, as a convenience, sorts `set`s when they're echoed in the REPL. You can override that behavior by explicitly calling `print()` on them, so `set`'s built-in `repr` is used, not the pretty-printed form IPython tries to use. – ShadowRanger Oct 20 '21 at 04:56
  • @Stack: That answer is wildly out-of-date at this point, thanks to *many* changes to `dict` (key-sharing `dict`s, insertion-ordered `dict`s, etc.). – ShadowRanger Oct 20 '21 at 04:58

0 Answers0