0

I have a string

x = "acbbadb"

After applying set to x

set(x)

I am getting

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

How is the function deciding the sequence of characters. I want it to print the character in the order in which it is appearing in the string like

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

How can I do it?

DavidG
  • 21,958
  • 13
  • 81
  • 76
Himanshu Poddar
  • 3,438
  • 5
  • 34
  • 67
  • You wanna check out the following post about https://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set – pissall Aug 23 '18 at 11:12
  • 1
    @RahulKP that will not help. sets are not ordered. – hiro protagonist Aug 23 '18 at 11:14
  • you could always use `set` to remove duplicates and then sort in the order of your input string: `x = tuple(sorted(set("acbbadb"), key=lambda x: "acbbadb".index(x)))` (sorry, ugly one-liner...) – hiro protagonist Aug 23 '18 at 11:20

0 Answers0