1

I would like to place 'y' value at the end of the set {'e', 'y', 'u'}.

I have tried to remove the value and then add it again at the end:

last_vowels = 'y'
vowels = {'e', 'y', 'u'}
vowels.remove(last_vowels)
vowels.add(last_vowels)
print(vowels)

I've also tried to convert it into the list and then append 'y' it at the end, and convert it back to "set" type.

vowels.remove(last_vowels)
vowels_list = list(vowels)
vowels_list.append(last_vowels)
vowels = set(vowels_list)
print(vowels)

From what I read the "set" type doesn't have indexing.

So I'm wondering if there is a way to rearrange the 'set'?

Gооd_Mаn
  • 343
  • 1
  • 16
  • 4
    If you look at the documentations of [sets](https://docs.python.org/2/library/sets.html), it is clearly stated that sets are **Unordered collections of unique elements** – yatu Nov 12 '19 at 11:33
  • 1
    If you have all the vowels and want to maintain an order, you can check out [frozenset](https://docs.python.org/3/library/stdtypes.html#frozenset). Note, frozensets are immutable, so `add` `remove` not available. – Sayandip Dutta Nov 12 '19 at 11:38
  • @SayandipDutta You might post an answer instead of a comment. I tried what you suggested; didn't work. Maybe I used it incorrectly. If you show how to use your idea, it would be a great answer. – anatolyg Nov 12 '19 at 11:48
  • @SayandipDutta, thank you for your answer, but the frozen sets are not ordered as well (the elements can be set at any index). – Gооd_Mаn Nov 12 '19 at 11:50
  • 1
    From this answer: https://stackoverflow.com/questions/1867861/how-to-keep-keys-values-in-same-order-as-declared, I understand that the order of a `dictionary` can be manipulated (by the order you created it originally). So maybe you can work with the keys of the dictionary as the `set` you want (and give some arbitary values). – Aryerez Nov 12 '19 at 12:02
  • 1
    @Gооd_Mаn I am sorry, I conflated frozensets and SortedSets.I will write an answer. – Sayandip Dutta Nov 12 '19 at 12:11
  • @SayandipDutta, no problem at all! Thank you for your participation. – Gооd_Mаn Nov 12 '19 at 12:12
  • 1
    As I can't write an answer anymore, you may check this out: [`SortedSet`](http://www.grantjenks.com/docs/sortedcontainers/sortedset.html#sortedcontainers.SortedSet). To maintain order, you can perform the ordering in a list, and put `list.index` as `key` while creating `SortedSet`. – Sayandip Dutta Nov 12 '19 at 12:14

2 Answers2

2

Sets are un-ordered. This means that the order of elements cannot be manipulated.

LukasP
  • 96
  • 3
0

If you just want to sort it (I assume you are wanting these in alphabetical order) for output reasons, or make it a set to removes duplicates and then don't mind having it as a list then you can do:

my_set = {'e', 'y', 'u'}
print(sorted(my_set))
> ['e', 'u', 'y']
PyPingu
  • 1,697
  • 1
  • 8
  • 21
  • Thank you for your answer, but I need to keep it as a 'set'. – Gооd_Mаn Nov 12 '19 at 11:53
  • Why though? If you expand on what you need this for, and why you want it ordered, then it would be easier to help. In your example you are simply printing it, for which there is no reason to require a set. – PyPingu Nov 12 '19 at 11:55