-3

I was working on a problem which required the use of python's set function to be applied onto strings. This is what I observed which is baffling me. Please do tell me a reason as to why it occurs if anyone does know.

>>> set('123456789')
{'2', '6', '7', '5', '9', '1', '8', '4', '3'}
>>> set('123456789')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}
>>> set('132457689')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}
>>> set('122333444455555666666777777788888888999999999')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}
>>> >>> set('1223334444555556666667777777888888889999999991324354657869874645231232435465768986753514324354657698587647536425312635746587873562413237485769546372641495565364319483726415523648759648573462')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}

Despite the number of times or the order in which the elements(1-9) in the string are passed, they seem to get arranged in this strange pattern after execution.

So I thought to generalize this and see. Here is what I observed when I applied it on a-z and 0-9 all combined

>>> set('1234567890qwertyuiopasdfghjklzxcvbnm')
{'r', 'p', '9', '8', 'l', 'c', 't', 's', '5', 'g', '0', '4', 'q', 'b', 'a', 'i', 'x', 'w', 'o', 'z', 'n', 'm', '2', '6', 'u', 'j', '7', 'd', '1', 'e', 'v', 'k', '3', 'f', 'y', 'h'}
>>> set('1234567890qwertyuiopasdfghjklzxcvbnm'[::-1])
{'r', 'p', '9', '8', 'c', 'l', 't', 's', '5', 'g', '0', 'q', '4', 'b', 'a', 'i', 'x', 'w', 'o', 'z', 'n', 'm', '2', '6', 'j', 'u', '7', 'd', '1', 'e', 'v', 'k', '3', 'f', 'y', 'h'}

The order comes out to be different. 'c' & 'l' are flipped. '4' & 'q' are flipped. 'j' & 'u' are flipped.

But something even crazier is this

>>> set('1234567890qwertyuiopasdfghjklzxcvbnm') ==  set('1234567890qwertyuiopasdfghjklzxcvbnm'[::-1])
True

Any information about why this behavior occurs is much appreciated. Thank you.

  • 2
    This is the heading of [`sets` page](https://docs.python.org/2/library/sets.html) in python documentation: `8.7. sets — Unordered collections of unique elements` – Sayandip Dutta Jan 31 '20 at 06:28

2 Answers2

0

set works like in mathematics. It is unsorted, so you don't get the elements in order. You can't index it. As it is unsorted, a reversed set is exactly the same as the original. Sets also have unique items, so there can be only one item of its kind. If you are looking for a sorted, non-unique collection, use lists or tuples.

CoderCharmander
  • 1,862
  • 10
  • 18
  • But even if it is unsorted, why does it get represented in this particular order everytime? Is it dependent on some random state variable due to which each of the elements are the order we see? – Neel Shukla Jan 31 '20 at 06:30
  • And also if that is so, why does it not work when I used characters and numbers together? – Neel Shukla Jan 31 '20 at 06:31
  • I don't know how does that work. But `set`is unsorted. You may look into Python's source code. – CoderCharmander Jan 31 '20 at 06:33
  • @NeelShukla, you can fine the answer here - https://stackoverflow.com/questions/21701618/why-python-set-displays-in-same-order-if-sets-are-unordered – anuragal Jan 31 '20 at 06:38
  • To add to @anuragal post above, here is a detailed explanation on Python's hashing via `pyhash.h` implementation at the source level: https://stackoverflow.com/a/37614182/1305461 – felipe Jan 31 '20 at 06:40
0

Python Set Type

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)

Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

As stated above Set is an unordered collection, which means it does not maintain the order of insertion. Because of that all your examples show one or other pattern

In the last example you are reversing the Set but elements are same. Two sets are equal, if and only if every element of each set is contained in the other (each is a subset of the other). Because of that you are getting True.

anuragal
  • 3,024
  • 19
  • 27