Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Python has an ordered dictionary. What about an ordered set?

1 Answer

0 votes
by (106k points)

Yes, python has an ordered set and they run on Py2.6 or later and 3.0 or later without any modifications. The interface is almost exactly the same as a normal set, except that initialisation should be done with a list.

OrderedSet([1, 2, 3])

This is a MutableSet, so the signature for .union doesn't match that of set, but since it includes __or__ something similar can easily be added:

@staticmethod 

def union(*sets): 

   union = OrderedSet() 

   union.union(*sets) 

   return union 

def union(self, *sets):

   for set in sets: 

       self |= set

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Oct 3, 2019 in Python by Tech4ever (20.3k points)
0 votes
1 answer
asked Jul 22, 2019 in Python by Eresh Kumar (45.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...