1

When I send a string as a argument in set data structure in python, surprisingly the order has been changed for some characters.

Suppose, If i write

>>> c=set('abcd') 

Then I should expect the set c to be shown as {'a', 'b', 'c', 'd'} but it shows output like that:-

>>> c
{'c', 'b', 'd', 'a'}

And more importantly, as far i know set in python only accept an object that is iterable.

So, it is obvious that set would iterate through the string and should maintain the correct order of the string.

I have tested it several times. And every times the order has been changed surprisingly....

enter image description here

I know in python there is nothing called character. A single character is also defined as string. But, since iteration occurs through the string this should be ordered.

So, could someone explain me the reason of the scenario? Is there any insight
in the iteration process or python VM issues ?

* I intended to know the iteration methodology of strings in set, not dict*

S.Rakin
  • 812
  • 1
  • 11
  • 24
  • 1
    As for your question on _why_ it doesn't preserve order, see [Why is the order in dictionaries and sets arbitrary?](http://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary) – roganjosh May 03 '17 at 10:22
  • @roganjosh ... Thanks.. I got what I asked for. – S.Rakin May 03 '17 at 10:25

4 Answers4

2

Sets, like dicts, are not ordered. Any iterable, added to a set, will not preserve order.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • can you please explain why it will not preserver order ?? when i am sending any list or tuple in the set then It maintain proper order. So why not String? – S.Rakin May 03 '17 at 10:18
  • 1
    No it does not - or if it does, it is only by coincidence. All iterables work the same when converted to sets, because sets operate on the *hashed* values of the members, not the actual values. – Daniel Roseman May 03 '17 at 10:19
1

Refers to the set python docs here https://docs.python.org/3/tutorial/datastructures.html#sets you can see also a brief demo code.

e.arbitrio
  • 556
  • 4
  • 14
  • Thanks. Seen that. But my question is why it's not preserving the order ? Isn't that easy for iterator to iterate in a order ?? :( :( – S.Rakin May 03 '17 at 10:21
1

If you want unique characters while still preserving the order of first seen letters, you could use an OrderedDict:

from collections import OrderedDict

unique_letters = OrderedDict((k, True) for k in 'abcdabcdabeddecd')
print(unique_letters.keys())
# ['a', 'b', 'c', 'd', 'e']

Sets have one very important job: to tell if an element is included in the set, and to tell it as fast as possible. Keeping the insertion order isn't on the set todo-list.

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
1

Python store set elements in optimized way so that it can apply operations with lesser runtime complexity i.e. less number of iterations will required in Union/Intersection by Python internally.