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
  • 35,525
  • 9
  • 94
  • 135
Sandesh34
  • 269
  • 1
  • 2
  • 13

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
  • 77,832
  • 6
  • 59
  • 90
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
  • 78,594
  • 17
  • 67
  • 103