A set in Python is a linear data structure used to store elements without allowing duplicates. But it doesn’t allow indexing.
A set that allows numerical indexing of the elements is called IndexableSet
and is provided by the sortedcollections
module.
IndexableSet
follows zero-based indexing.
IndexableSet(iterable)
iterable
: This is an iterable of elements.The method returns a set whose elements can be accessed via indexes.
from sortedcollections import IndexableSetiterable = '85678756's = IndexableSet(iterable)print("Set - ", s)print("Elements from index 0 to 2 - ", s[:2])print("Element at index 3 - ", s[3])
IndexableSet
from the sortedcollections
package.IndexableSet()
with the iterable defined in line 3.0
to 2
.3
.Free Resources