1

I'm just trying to learn set containers in python. So, I created a normal set using :

dset = set(['a','c','z','d'])

but when I print it, it results :

{'c', 'a', 'z', 'd'}

shouldn't it be :

{'a', 'c', 'z', 'd'}

Can anyone explain why the output is like this? Is there any default sort mechanism for set datatype? or is my math is weak?

elpidaguy
  • 624
  • 1
  • 11
  • 25
  • 4
    sets are not ordered containers. If you want order you have to go with lists or tuples – Ma0 Oct 16 '18 at 07:11
  • so they'll always give this random ordered output everytime I print a newly created set? @Ev.Kounis – elpidaguy Oct 16 '18 at 07:12
  • 1
    It could be that you get the same output if you run it over and over again but this output is not one you can count on getting. – Ma0 Oct 16 '18 at 07:15
  • 1
    I think [this](https://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set) is what you want. There is a module in pypi [ordered-set](https://pypi.org/project/ordered-set/) – Albin Paul Oct 16 '18 at 07:17
  • so I'll conclude that as set are unordered containers, It will randomly print the items in it. Not any logical explanation to it? am i correct? and Thanks! @Ev.Kounis – elpidaguy Oct 16 '18 at 07:19
  • 1
    @exabytes.js - Because of their primary usage, there is no need for there to be an order and it assists performance. See [Python Sets vs Lists](https://stackoverflow.com/q/2831212/1324033) – Sayse Oct 16 '18 at 07:21
  • 2
    @Sayse yes, this helped. Thank you! – elpidaguy Oct 16 '18 at 07:24

2 Answers2

2

Sets are not ordered, they are not ordered containers, so nothing you can do,

If you have a list and you want to use set then convert back to list in ordered way:

print(sorted(set(l),key=l.index)) 

So no chance of making ordered set

Btw, there is a ordered set...

link to download file, or just copy the code in a module and import it, remember to remove the if __name__ == '__main__' part in the bottom

Or pip install boltons, then:

from boltons.setutils import IndexedSet

Then example:

>>> from boltons.setutils import IndexedSet
>>> x = IndexedSet(list(range(4)) + list(range(8)))
>>> x
IndexedSet([0, 1, 2, 3, 4, 5, 6, 7])
>>> x - set(range(2))
IndexedSet([2, 3, 4, 5, 6, 7])
>>> x[-1]
7
>>> fcr = IndexedSet('freecreditreport.com')
>>> ''.join(fcr[:fcr.index('.')])
'frecditpo'

See link

Or pip install sortedcontainers:

Once installed you can simply:

from sortedcontainers import SortedSet
help(SortedSet)

Or install collections_extended

Then example:

>>> from collections_extended import setlist
>>> sl = setlist('abracadabra')
>>> sl
setlist(('a', 'b', 'r', 'c', 'd'))
>>> sl[3]
'c'
>>> sl[-1]
'd'
>>> 'r' in sl  # testing for inclusion is fast
True
>>> sl.index('d')  # so is finding the index of an element
4
>>> sl.insert(1, 'd')  # inserting an element already in raises a ValueError
ValueError
>>> sl.index('d')
4

See link

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

From the doc (https://docs.python.org/3.7/library/stdtypes.html#set-types-set-frozenset):

A set object is an unordered collection of distinct hashable objects.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118