I have a function that has takes in a string "secret"

sec = set(secret)
print sec

output: set(['A', 'C', 'E', 'I', 'H', 'M', 'S', 'T'])

Assuming I called the function sending in the string "Mathematics", how do I get an ordered set. I'm looking to get unique characters in the original element order. In other words, I want the output to be "MATHETICS" not "ACEIHMST"

Recommended Answers

Try this ...

'''
wants
"MATHEICS" not "ACEIHMST"
'''

s = "Mathematics".upper()

s3 = ""
for c in s:
    if c not in s3:
        s3 += c

print(s3)  # MATHEICS
Jump to Post

All 3 Replies

Try ''.join(sorted(sec))

Try this ...

'''
wants
"MATHEICS" not "ACEIHMST"
'''

s = "Mathematics".upper()

s3 = ""
for c in s:
    if c not in s3:
        s3 += c

print(s3)  # MATHEICS
>>> from collections import OrderedDict
>>> s = "Mathematics".upper()
>>> ''.join(OrderedDict.fromkeys(s))
'MATHEICS'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.