In Python, a set is a built-in data structure that only stores unique items.
Similar to OrderedDict, the OrderedSet maintains the insertion order of the elements.
The OrderedSet() function is provided by the sortedcollections module. This can be installed using pip:
pip install sortedcollections
OrderedSet(iterable)
iterable: This is the iterable to be converted to OrderedSet. The iterable can be a set, list, and so on.This method returns an instance of OrderedSet.
Let’s look at an example:
from sortedcollections import OrderedSetiterable = "hello-educative"ordr_set = OrderedSet(iterable)print(ordr_set)
OrderedSet from the sortedcollections module.iterable.ordr_set by passing the iterable as an argument to the OrderedSet constructor.ordr_set.From the output, we can observe that no duplicate entries are present in the set, and the insertion order is preserved.
Free Resources