What is the OrderedSet() in Python?

Set in Python

A set in Python is a linear data structure we use to store elements without allowing duplicates.

A set that maintains the insertion order of the elements is called the OrderedSet. We can find it in the sortedcollections module.

Syntax

OrderedSet(iterable=())

Parameter

  • iterable: This is an iterable of elements.

Return value

The method returns a set that maintains the insertion order of the elements.

Code

from sortedcollections import OrderedSet
iterable = '85678756'
s = OrderedSet(iterable)
print(s)

Explanation

  • Line 1: We import the OrderedSet from the sortedcollections package.
  • Line 3: We define an iterable.
  • Line 4: We define an instance of OrderedSet() with the defined iterable.
  • Line 5: We print the ordered set.

Free Resources