0

I want to print the elements of the set consecutively, so I wrote the following code:

s='dmfgd'
print(set(s))

However, this code shows the output as:

set(['m', 'd', 'g', 'f'])

but, I want output like:

set(['d','m','f','g'])

Any help will be appreciated.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Sandesh34
  • 279
  • 1
  • 2
  • 14

2 Answers2

4

Set is unordered. You can instead use a list of dict keys to emulate an ordered set if you're using Python 3.6+:

print(list(dict.fromkeys(s)))

This outputs:

['d', 'm', 'f', 'g']
blhsing
  • 91,368
  • 6
  • 71
  • 106
1

Python set is unordered collections of unique elements

Try:

s='dmfgd'

def removeDups(s):
    res = []
    for i in s:
        if i not in res:
            res.append(i)
    return res

print(removeDups(s))

Output:

['d', 'm', 'f', 'g']
Rakesh
  • 81,458
  • 17
  • 76
  • 113