0

I have the string 'ABBA'. If I turn it into a set, I get this:

In: set('ABBA')
Out: {'A', 'B'}

However, if I join them as a string, it reverses the order:

In: ''.join(set('ABBA'))
Out: 'BA'

The same happens if I try to turn the string into a list:

In: list(set('ABBA'))
Out: ['B', 'A']

Why is this happening and how do I address it?

EDIT

The reason applying sorted doesn't work is that if I make a set out of 'CDA', it will return 'ACD', thus losing the order of the original string. My question pertains to preserving the original order of the set itself.

snapcrack
  • 1,761
  • 3
  • 20
  • 40
  • 1
    Sets are not ordered. If you want a specific order you need to sort or use something else. – Mark Jun 16 '21 at 04:01
  • 2
    A `set` is an unordered collection with no duplicate elements – Rakesh Jun 16 '21 at 04:01
  • Which version of Python? More recent versions of Python have more predictable orders for dictionaries, and that might bubble down to sets as well. – Mark Ransom Jun 16 '21 at 04:05
  • @MarkRansom it doesn't, not even in the latest python. – Mark Jun 16 '21 at 04:07
  • 1
    you may want to use `collections.Counter` to keep the original order. Returns a dict though. Also, `OrderedSet()` is another option, pls check [here](https://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set) – nikn8 Jun 16 '21 at 04:09
  • 3
    It recent versions of python you can take advantage of the fact that dictionaries preserve insertion ordering: `list(dict.fromkeys("ABBA"))` – Mark Jun 16 '21 at 04:09
  • @Mark I don't understand why the improvements to `dict` don't apply to `set` as well, but I really like your alternative. You should turn it into an answer. – Mark Ransom Jun 16 '21 at 04:12
  • @MarkRansom I don't know the reasoning either. Maybe it's hard to maintain the expected performance of sets and preserve the order. – Mark Jun 16 '21 at 04:13
  • Hi, do check out how it's giving output in latest version, `list(set('ABBA'))` is giving me `['A', 'B']` in `Python 3.9.5` – p2pdops Jun 16 '21 at 04:13

2 Answers2

1

Sets are unordered collection i.e. you will get a different order every time you run the command and sets also have unique elements therefore there will be no repetition in the elements of the set. if you try running this command for a few times set('ABBA') sometimes you will get the output as {'A', 'B'} and sometimes as {'B', 'A'} and that what happens when you are using the join command the output is sometimes taken as BA and sometimes it will show AB.

There is an ordered set recipe for this which is referred to from the Python 2 Documentation. This runs on Py2.6 or later and 3.0 or later without any modifications. The interface is almost exactly the same as a normal set, except that initialisation should be done with a list.

OrderedSet([1, 2, 3])

This is a MutableSet, so the signature for .union doesn't match that of set, but since it includes or something similar can easily be added

1
b = "AAEEBBCCDD"
a = set(b)#unordered
print(a)#{'B', 'D', 'C', 'A', 'E'}/{'A', 'E', 'B', 'D', 'C'}/,,,
#do not have reverses the order,only random
print(''.join(a))

print(list(a))

print(sorted(a, key=b.index))#Save original sequence(b)
ajj_9814
  • 11
  • 1